【问题标题】:ERROR: Cannot read property '_id' of undefined错误:无法读取未定义的属性“_id”
【发布时间】:2014-03-07 22:45:02
【问题描述】:

我正在尝试使用 mongoose.js 框架方法 model.Create 创建记录,如下例所示,我收到一个错误 Cannot read property '_id' of undefined

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);

    })

我的代码有什么问题吗?任何帮助表示赞赏。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    如果您更改行(将 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。

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2020-09-03
      • 2017-06-15
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2022-07-27
      • 2013-09-03
      • 2022-09-23
      相关资源
      最近更新 更多