My code contains 2 models, Content and Movie. Movie has a reference to Content. In 'Movie' query when i try to populate Content', null is returned.
Content Model
'use strict';
var mongoose = require('mongoose'),
validators = require('./validators'),
Schema = mongoose.Schema;
var contentType = ['M'];
var ContentSchema = new Schema({
createdOn: {
type: Date,
default: Date.now
},
name: {
type: String,
required: true,
unique: true,
trim: true
},
type: {
type: String,
required: true,
enum: contentType,
trim: true
},
releaseDate: {
type: Date
},
genres: [{
type: Schema.Types.ObjectId,
ref: 'Genre'
}],
language: {
type: Schema.Types.ObjectId,
ref: 'Language'
},
imagePath: {
type: String
},
filePath: {
type: String
}
});
mongoose.model('Content', ContentSchema);
Movie Model
var mongoose = require('mongoose'),
validators = require('./validators'),
Schema = mongoose.Schema;
var MovieSchema = new Schema({
createdOn: {
type: Date,
default: Date.now
},
contentId: {
type: Schema.Types.ObjectId,
ref: 'Content'
},
cast: {
type: [String]
},
synopsis: {
type: String
},
length: {
type: Number
},
director: {
type: String
}
});
mongoose.model('Movie', MovieSchema);
This is my controller on server side:
Movie.find().populate('contentId').exec(function (err, movies) {
if (err) {
return res.json(500, {
error: 'cannot list movies'
});
}
res.json(movies);
});
Output:
[
{
"_id": "540dcdda98fcaefbaf26fd72",
"contentId": null,
"synopsis": "Nice Movie",
"length": 140,
"director": "Foo Bar",
"cast": [],
"createdOn": "2014-09-08T16:38:19.275Z"
}
]
Why is the output null even though I can see contentId in output if i remove populate from my query.
Nevermind, figured out the issue.
There's no problem with code. I had my data entry in 'content' table instead of 'contents' table. Sigh.