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.

Adiv Ohayon
  • 21
  • 1
  • 4
  • 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. – Brian Shamblen Jun 02 '15 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? – Adiv Ohayon Jun 02 '15 at 16:55
  • Which version of mongoose are you using? – Brian Shamblen Jun 02 '15 at 17:52
  • @BrianShamblen "mongoose": "~3.8.8", – Adiv Ohayon Jun 03 '15 at 08: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. – Kesarion Aug 31 '16 at 10:44
  • In my case, I was passing the `'ref'` for another Schema – Shivam Jha Mar 11 '21 at 16:40

1 Answers1

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'})
  • 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)? – Maxime Flament Jan 30 '19 at 09:41