Node.js mongodb增量编号-500内部服务器错误

Node.js mongodb增量编号-500内部服务器错误,node.js,mongodb,express,Node.js,Mongodb,Express,我正在尝试使用node和mongodb对文档的id进行编号。我收到一个500内部服务器错误:“无法读取null的属性'seq' //提交票据-返回500错误-不更新ID router.post('/ticketform', function(req, res){ var db = req.db; function getNextSequence(name, callback){ db.counters.findAndModify( { q

我正在尝试使用node和mongodb对文档的id进行编号。我收到一个500内部服务器错误:“无法读取null的属性'seq'

//提交票据-返回500错误-不更新ID

router.post('/ticketform', function(req, res){
    var db = req.db;

function getNextSequence(name, callback){
    db.counters.findAndModify(
        {
            query:{ _id:name},
            update:{$inc:{seq:1} },
            new:true
        }, callback
    );
}

    getNextSequence('ticket', function(err, obj) {
        if (err) console.error(err);
        db.users.insert(
        {
            '_id': obj.seq,
            'firstname':req.body.firstname,
            'lastname':req.body.lastname,
            'email':req.body.email,
            'phone':req.body.phone,
            'issue':req.body.issue
        }, function(err,docs) {
            if (err) console.error(err);
            console.log(docs);
            res.end();
        });
    });
});

你可能指的是。在本例中,他们展示了MongoDB shell中的实现,其中findAndModify是同步的。正如JohnnyHK在评论中指出的,Node.js中的findAndModify是异步的,结果在回调中返回

在Node.js中,您可以执行以下操作:

function getNextSequence(name, callback){
    db.counters.findAndModify(
        { "_id": "ticket" },
        [],
        { "$inc": { "seq": 1 }},
        { "new": true, "upsert": true },
        callback
    );
}

getNextSequence('ticket', function(err, obj) {
    if (err) console.error(err);
    db.users.insert(
    {
        '_id': obj.seq,
        'firstname':req.body.firstname,
        'lastname':req.body.lastname,
        'email':req.body.email,
        'phone':req.body.phone,
        'issue':req.body.issue
    }, function(err,docs) {
        if (err) console.error(err);
        console.log(docs);
        res.end();
    });
});

您还应该检查命令以更好地理解选项。

您似乎使用的是节点本机驱动程序,其中的语法与mongodb shell稍有不同。该函数的工作方式特别不同

作为一个完整的工作示例,您可以在应用程序中使用以下代码,再加上异步模块的使用,使逻辑看起来更清晰:

var async = require("async"),
    mongodb = require("mongodb"),
    MongoClient = mongodb.MongoClient;


MongoClient.connect('mongodb://localhost/test',function(err,db) {

    async.waterfall(
        [
            function(callback) {
                db.collection("counters").findAndModify(
                    { "_id": "ticket" },
                    [],
                    { "$inc": { "seq": 1 } },
                    { "upsert": true, "new": true },
                    function(err,doc) {
                        callback( err, doc.seq );
                    }
                );
            },
            function(seq, callback) {
                var doc = { "_id": seq };
                db.collection("users").insert(
                    { "_id": seq },
                    function(err, doc) {
                      callback( err, doc );
                    }
                );
            }
        ], function( err, result ) {

            console.log( result );
        }
    );
});
函数上的参数是“查询”、“排序”,这是一个数组,即使为空也是必需的,然后是“更新”文档、“选项”,您希望在其中返回更新后的文档,以及“upsert”以在不存在匹配文档的情况下创建新文档, 最后是回调

异步瀑布允许您“传递”结果,而无需将功能嵌入用于获取序列的方法的回调中。您不需要这样做,但是代码看起来更简洁直接


或者,如果您想使用在中具有帮助器功能的工具,也可以使用mongoose等选项。使用该库,您可能会发现连接管理更容易一些,但这取决于您。

是异步的,因此它不会返回结果,而是将结果提供给回调。@MichaelSpatafore修复了我的查询