Check out this schema design:
var messageSchema = new Schema({
receivers: [User],
message: String,
owner: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
var userSchema = new Schema({
name: String,
photo: String
});
var inboxSchema = new Schema({
messages: [Message],
owner: {
type: Schema.Types.ObjectId,
required: true
},
sequence: Number
});
When attempting to create a new message with the following code:
var newMsg = {
receivers: ['<id1>', '<id2>'],
message: 'Hello world',
owner: '<userId>'
}
Inbox.findOneAndUpdate({
owner: '<userId>'
}, {
$push: {
messages: newMsg
},
$set: {
sequence: '<sequence>'
}
}, {
upsert: true
}, callback);
An error is thrown:
MongooseError.CastError "Cast to undefined failed for value [object Object] at path "messages""
What could be causing this issue? How can it be resolved?
Appreciate any help.