Tam2
Tam2

Reputation: 1367

Mongoose - Cast to ObjectId failed for value "[object Object]" at path _id

I have the below schema:

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 },        
})

I've got the below which does the query:

 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"
                })
            }
        })
}

As above if I add .populate('users') it flags the below error:

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

I'm calling /view/51fc2e02576f2dc058000001 (which is an Object ID of the stories table), without the .populate('users') if I call the URL it brings back the document.

The users -> id value is populated with ObjectId("51fbe87ec137760025000001") - which is a valid _id in the users collection

I cannot see what I'm missing?

Added User Schema

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

Upvotes: 2

Views: 8955

Answers (1)

zs2020
zs2020

Reputation: 54551

I think you can only do .populate('users.id'). Populate is to use the reference Object to replace the id field. Please take a look at the doc.

Upvotes: 4

Related Questions