猫鼬/节点错误:无法读取属性' ObjectId'未定义的

时间:2016-01-28 23:27:29

标签: node.js mongodb

我正在创建一个小节点/ express / mongo应用程序,它允许用户发布猫照片并对其进行评论。我有两个模型SELECT SUM( IF(t.pd1 BETWEEN ? AND ?, t.pa1, 0) + IF(t.pd2 BETWEEN ? AND ?, t.pa2, 0) + IF(t.pd3 BETWEEN ? AND ?, t.pa3, 0) + IF(t.pd4 BETWEEN ? AND ?, t.pa4, 0) + IF(t.pd5 BETWEEN ? AND ?, t.pa5, 0) + IF(t.pd6 BETWEEN ? AND ?, t.pa6, 0) + IF(t.pd7 BETWEEN ? AND ?, t.pa7, 0) ) AS `total` FROM mytable t cat。一切都工作正常,直到我决定将两个模型关联在一起,然后导致了这个错误:

comment

错误是指猫模型:

type: mongoose.Schema.Type.ObjectId,
                            ^ 
TypeError: Cannot read property 'ObjectId' of undefined

以下是评论模型:

var mongoose = require('mongoose');


var catModel = mongoose.Schema({
    name: String,
    image: String,
    owner: String,  
    description: String,
    comments: [

        {
            type: mongoose.Schema.Type.ObjectId,
            ref: "Comment"

        } 
    ]
});

var Cat = mongoose.model("Cats", catModel);

module.exports = Cat;

以下是app.js的片段:

var mongoose = require('mongoose');

var commentSchema = mongoose.Schema({

    username: String,
    content: String,
});


Comment = mongoose.model('Comment', commentSchema);

module.exports = Comment;

我正在使用猫鼬4.3.7。我研究过这个问题而无法解决。例如,我查看了this帖子并重新安装了mongoose,但问题仍然存在。

2 个答案:

答案 0 :(得分:8)

这是一个错字,因为没有Type SchemaTypes。它应该是comments: [{ "type": mongoose.Schema.Types.ObjectId, "ref": "Comment" }] 而不是:

{{1}}

答案 1 :(得分:4)

问题在于你的架构中的注释类型,但它看起来很好但只是尝试:

  comments:[{ type: String, ref: 'Comment' }],

  comments: [{type: Schema.ObjectId, ref: "Comment"}