2

我有以下架构:

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 (这是 stories 表的对象 ID),.populate('users')如果我调用 URL,它会带回文档。

users -> id 值填充有ObjectId("51fbe87ec137760025000001")- 这是 users 集合中的有效 _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 }              
})
4

1 回答 1

4

我认为你只能这样做.populate('users.id')填充是使用引用对象来替换id字段。请看一下文档。

于 2013-08-02T22:47:06.943 回答