Node, Mongoose js - Populate never returning a result

153 Views Asked by At

I'm new to MongoDB / Mongoose, and I'm attempting to match up a user with a site collection. The idea is that the user is assigned to a site. I want to obtain this username based on an id stored in the sites collection, and then display it. The username can change, so I can't use subdocs.

On attempting to bind the username to my query using populate, the query never returns a result. No errors, no nothing. One could call it a hang, but it's async, so whatever mongo is doing in the background I don't know.

My authentication system is passport.js.

Here is my models:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

var Account = new Schema({
    username: String,
    password: String,
    isadmin: Boolean,
    islocked: Boolean
});

Account.plugin(passportLocalMongoose);

module.exports = mongoose.model('Account', Account);


var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var siteSchema = new Schema({
    title: String,
    locked: Boolean,
    userid: {type: mongoose.Schema.Types.ObjectId, ref: 'Account'}
});

var Site = mongoose.model('Site', siteSchema);

module.exports = Site;

And here is my query:

Site.find({}.populate('userid'), function (err, postIn) {
    if (err) {
        throw err
    } else {
        res.send(postIn);
    }
});

Any ideas - been stuck on this for a while.

1

There are 1 best solutions below

3
On BEST ANSWER

use .exec. It configures the populate.

Site.find({}).populate('userid').exec(function (err, postIn) {
        if (err) {
            throw err
        } else {
            res.send(postIn);
        }
    });