当我运行下面的简单代码来插入新文档时:
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
其中包含插入的文档/文档。
Try:
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);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)