我的代码包含2个模型,内容和电影。电影提到了内容。 在电影'查询当我尝试填充内容'时,返回null。
内容模型
'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);
电影模型
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);
这是我服务器端的控制器:
Movie.find().populate('contentId').exec(function (err, movies) {
if (err) {
return res.json(500, {
error: 'cannot list movies'
});
}
res.json(movies);
});
输出:
[
{
"_id": "540dcdda98fcaefbaf26fd72",
"contentId": null,
"synopsis": "Nice Movie",
"length": 140,
"director": "Foo Bar",
"cast": [],
"createdOn": "2014-09-08T16:38:19.275Z"
}
]
为什么输出为null,即使我在输出中看到contentId,如果我从查询中删除填充。
答案 0 :(得分:2)
没关系,弄明白了这个问题。
代码没问题。我在'内容'中输入了数据。表而不是'内容'表。叹息。