2

so I've been at it for like 4 hours, read the documentation several times, and still couldn't figure out my problem. I'm trying to do a simple populate() to my model. I have a User model and Store model. The User has a favoriteStores array which contains the _id of stores. What I'm looking for is that this array will be populated with the Store details.

user.model

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

var UserSchema = new Schema({
      username: String,
      name: {first: String, last: String},
      favoriteStores: [{type: Schema.Types.ObjectId, ref: 'Store'}],
      modifiedOn: {type: Date, default: Date.now},
      createdOn: Date,
      lastLogin: Date
});

UserSchema.statics.getFavoriteStores = function (userId, callback) {
    this
    .findById(userId)
    .populate('favoriteStores')
    .exec(function (err, stores) {
        callback(err, stores);
    });
}

And another file:

store.model

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

var StoreSchema = new Schema({
  name: String,
  route: String,
  tagline: String,
  logo: String

});

module.exports = mongoose.model('Store', StoreSchema);

After running this what I get is:

{
    "_id": "556dc40b44f14c0c252c5604",
    "username": "adiv.rulez",
    "__v": 0,
    "modifiedOn": "2015-06-02T14:56:11.074Z",
    "favoriteStores": [],
    "name": {
        "first": "Adiv",
        "last": "Ohayon"
    }
}

The favoriteStores is empty, even though when I just do a get of the stores without the populate it does display the _id of the store.

Any help is greatly appreciated! Thanks ;)

UPDATE After using the deepPopulate plugin it magically fixed it. I guess the problem was with the nesting of the userSchema. Still not sure what the problem was exactly, but at least it's fixed.

6
  • 1
    Have you loaded the "store.model" module at least once, somewhere in your code, before calling the getFavoriteStores function? If not, the store model will not be registered with mongoose and the populate will fail. Commented Jun 2, 2015 at 15:29
  • @BrianShamblen Yes, I tried adding var Store = require('../store/store.model'); in both the model and the controller. Am I still missing something with regards to registering the store model? Commented Jun 2, 2015 at 16:55
  • Which version of mongoose are you using? Commented Jun 2, 2015 at 17:52
  • @BrianShamblen "mongoose": "~3.8.8", Commented Jun 3, 2015 at 8:43
  • If anyone else has this problem, you should try passing the model as well, like in this example: .populate({ path: 'favoriteStores', model: 'Store' }); // That solved it for me. Commented Aug 31, 2016 at 10:44

1 Answer 1

11

I think this issue happens when schemas are defined across multiple files. To solve this, try call populate this way:

.populate({path: 'favoriteStores', model: 'Store'})
Sign up to request clarification or add additional context in comments.

1 Comment

Solved the issue for me, do you know why this issue is happening? I mean, why is declaring models across multiple files raise this issue if the query is at database level (which doesn't know anything about multiple files definition)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.