AJTECH
AJTECH

Reputation: 387

Im trying to populate some user info onto the articles query

Mongo Populate

Im trying to populate some user info onto the articles query

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();
    });
};

Im getting the error

MissingSchemaError: Schema hasn't been registered for model "email".

Any ideas??

Here is the schema

'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);

Upvotes: 1

Views: 95

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312129

The third parameter to populate is the name of the model you wish to use for population, overriding what's specified in the schema.

Assuming email is a field you want from the user doc, include that in the second parameter instead:

exports.articleByID = function(req, res, next, id) {
    Article.findById(id).populate('user', 'displayName email').exec(...

Upvotes: 1

Related Questions