【问题标题】:'undefined is not a function' when using unordered bulk upsert使用无序批量 upsert 时“未定义不是函数”
【发布时间】:2015-04-21 23:30:21
【问题描述】:

我正在使用批量更新插入来一次更新/添加多个文档到我的数据库:

var bulk = collection.initializeUnorderedBulkOp();
docs.forEach(function(doc, index, array){
  bulk.find({'docId' : doc.docId}).upsert().updateOne(doc);
});
bulk.execute();

bulk.execute 这将返回以下错误:

/myPath/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:771
  catch(err) { process.nextTick(function() { throw err}); }
                                                   ^
TypeError: undefined is not a function
    at /myPath/node_modules/mongodb/lib/bulk/unordered.js:470:5

我查看了 mongodb 模块中的代码,这里的回调似乎失败了:

// Execute batches
return executeBatches(this, function(err, result) {
  callback(err, result);
});

数据正在完全按预期写入数据库,但仍然抛出此错误,我无法弄清楚我可以做些什么来导致它。

我已经排除了我的数据问题,方法是使用普通对象,并使用批量插入而不是 upsert,因为它们更简单,但结果是相同的。

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    你需要invoke bulk.execute with a callback

    bulk.execute(function(err,results) {
        if(err)
            console.error(err);
        else
            console.log(results);
    });
    

    或类似的,应该可以工作。

    【讨论】:

    • 谢谢,我只是想通了,我正在查看的文档中没有提到。
    猜你喜欢
    • 2014-07-17
    • 2021-12-13
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多