我正在尝试使用 mongoose.js 框架方法 model.Create 创建记录,如下例所示,我收到错误 无法读取未定义的属性“_id”
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', yourSchema);
var Aryobj = [{ size: 'small' }, {size:'large'}];
Tank.create(Aryobj, function (err, small, large) {
if (err) return handleError(err);
console.log("small ID:"+small._id);
console.log("large ID:"+large._id);
})
我的代码有什么问题吗?如有任何帮助,我们将不胜感激。
最佳答案
如果您更改行(将 yourSchema 替换为 schema),我认为这只是代码中的拼写错误
var Tank = mongoose.model('Tank', yourSchema);
至
var Tank = mongoose.model('Tank', schema);
因为定义是针对 var 架构的
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
我运行了脚本并纠正了拼写错误,它输出了两个 ID。
关于node.js - 错误: Cannot read property '_id' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22252324/