我试图填充一些用户信息到文章查询
Mongo Populate
我试图填充一些用户信息到文章查询
exports.articleByID = function(req, res, next, id) { Article.findById(id).populate('user', 'displayName', 'email').exec(function(err, article) { if (err) return next(err); if (!article) return next(new Error('Failed to load article ' + id)); req.article = article; next(); }); };
我得到错误
MissingSchemaError:架构尚未注册模型“电子邮件”。
有任何想法吗??
这是架构
'use strict'; /** * Module dependencies. */ var mongoose = require('mongoose'), Schema = mongoose.Schema; /** * Article Schema */ var ArticleSchema = new Schema({ created: { type: Date, default: Date.now }, title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, content: { type: String, default: '', trim: true }, user: { type: Schema.ObjectId, ref: 'User' } }); mongoose.model('Article', ArticleSchema);
要populate
的第三个参数是要用于populate
的模型的名称,覆盖模式中指定的内容。
假设email
是用户doc所需的字段,请将其包含在第二个参数中:
exports.articleByID = function(req, res, next, id) { Article.findById(id).populate('user', 'displayName email').exec(...