Mongoose填充返回null
我的代码包含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,如果我删除从我的查询填充。
没关系,搞清楚了这个问题。
代码没有问题。 我在“内容”表中input了数据,而不是“内容”表。 叹。