Mongoose - 对于路径_id的值“[object Object]”,转换为ObjectId失败

时间:2013-08-02 22:37:31

标签: node.js mongodb express mongoose

我有以下架构:

var StorySchema = new Schema({
  title:        { type: String, required: true },
  users:        { 
                  id:        { type: Schema.ObjectId, ref: 'users' },
                  creator:   { type: Boolean } 
                },       
  maxlines:     { type: Number, default: '10'},
  lines:        {
                  text:       { type: String },
                  entered_at: { type: Date }, 
                  user:       {
                                id:   { type: Schema.ObjectId, ref: 'users' }
                              }
                },
  created_date: { type: Date, default: Date.now }, 
  updated_date: { type: Date, default: Date.now },        
})

我有以下查询:

 exports.view = function (req, res) {  
    Stories
        .findOne({
            _id: req.params.id
        })
        /*.populate('users') */ **<-- If I uncomment this I get the error**
        .exec(function (err, story) {
            if (err) {
                res.json(200, {
                    success: "false",
                    message: err.message
                })
            } else if (story) {
                res.json({
                    sucess: "true",
                    message: story
                })
            } else {
                res.json(200, {
                    sucess: "false",
                    message: "story not found"
                })
            }
        })
}

如上所述,如果我添加.populate('users'),则会标记以下错误:

{
    "success": "false",
    "message": "Cast to ObjectId failed for value \"[object Object]\" at path \"_id\""
}

我正在调用/ view / 51fc2e02576f2dc058000001(这是故事表的对象ID),如果我调用它带回文档的URL,则没有.populate('users')

用户 - &gt; id值填充ObjectId("51fbe87ec137760025000001") - 这是用户集合中的有效_id

我看不出我错过了什么?

添加了用户架构

var UserSchema = new Schema({
  name:                 { type: String, required: true },
  email:            { type: String, required: true, unique: true },
  username:         { type: String, required: true, unique: true },
  provider:         { type: String, required: true, enum: ['local', 'facebook']    },
  password:         { type: String, required: true },
  avatar:           { type: String, default: 'http://i.imgur.com/1PtcFos.jpg' },
  gender:           { type: String, required: true, uppercase: true, enum: ['M', 'F'] },
  facebook:         {
                 id:     { type: String }, 
                 token:   { type: String },
                 token_expiry: { type: Date }
                        },
  device:       {
                  token:        { type: String },
                  type:         { type: String, enum: ['ios', 'android'] },
                  badge:        { type: Number },
                  id:           { type: String },
                  created_date: { type: Date, default: Date.now },
                  updated_date: { type: Date, default: Date.now }
                },              
  created_date: { type: Date, default: Date.now },
  updated_date: { type: Date, default: Date.now }              
})

1 个答案:

答案 0 :(得分:4)

我认为你只能做.populate('users.id')Populate是使用引用Object替换id字段。请查看文档。