我有关于nodejs + mongodb findandmodify()函数的问题,上周代码运行正常,但突然现在它抛出错误:
db.collection(collectionName).findAndModify(query || {}, [], {$set: object},
{upset: true}, callback || function () {
});
如果查询未定义代码运行正常,但查询params ex:“_ id”:“idofcollection”或“template”:“templatename”它抛出异常:
E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb\lib\db.js:309
handleCallback(callback, null, result.result);
^TypeError: Cannot read property 'result' of undefined
at E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb\lib\db.js:309:42
at E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb-core\lib\connection\pool.js:436:18
at doNTCallback0 (node.js:419:9)
at process._tickCallback (node.js:348:13)
请解释一下解决这个问题的方法吗?
答案 0 :(得分:1)
我会改用$or
:
db.collection(collectionName).findAndModify({ $or: [ query, { example: 10 } ] }, {$set: object}, function (){
});
两件事:
callback
是什么。通常回调是an anonymous function。query
应该看起来像{ price: 5 }
以下是$or
:https://docs.mongodb.com/manual/reference/operator/query/or/
希望这有帮助!