当我运行下面的简单代码插入一个新文档时:
collectionUsers.insert({'name':'john'},function(err,records){ if (err) throw err; console.log("Id of new document added = " + records[0]._id); });
我收到以下错误:
catch(err) { process.nextTick(function() { throw err}); } ^ TypeError: Cannot read property '_id' of undefined
回调函数不返回数组吗?我只是想获取新文档的id并将其保存在变量中以备将来使用.谢谢你的帮助.
或者,如果我使用insertOne,我得到此输出:添加新文档的ID = {"ok":1,"n":1}
但我想使用插入...
有一个ops
对象records
包含插入的doc/docs.
尝试:
collectionUsers.insert({'name':'john'},function(err,records){ // You can explore more here console.log("record contents",JSON.stringify(records,null,4)); // Desired output console.log("Id of new document added = " + records.ops[0]._id); });