Mongoose populate() returns empty array with no errors
Asked Answered
H

1

11

I've been trying to get this populate thing to work, but I'm getting issues because I am not getting the expected results, and no errors to work with. Just simply an empty array.

My models look like this. Each their own file

var mongoose = require( 'mongoose' );

var upgradeSchema = new mongoose.Schema({
  type: {
    type: String,
    default: "Any"
  },
  ability: String,
  ability_desc: String,
  level: Number,
  tag: String
});

mongoose.model('Upgrade', upgradeSchema);

and the other

var mongoose = require( 'mongoose' );
var crypto = require('crypto');
var jwt = require('jsonwebtoken');


var userSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
    required: true
  },
  hero: {
    level: Number,
    name: String,
    type: {
      path: String,
      heroType: String
    },
    upgrades: [{
      type: mongoose.Schema.Types.ObjectId, ref: 'Upgrade'
    }],
    unspent_xp: Number,
    total_xp: Number,
  },
  armyTotal: {
    type: Number,
    default: 0,
    max: 5000
  },
  army:[{
    foc_slot: String,
    unit_name: String,
    unit_cost: Number
  }],
  username: {
    type: String,
    required: true,
    unique: true,
  },
  faction: String,
  name: {
    type: String,
    required: true
  },
  hash: String,
  salt: String,
  roles: {
    type: String,
    default: 'player' }
});

And I'm trying to do this

module.exports.profileRead = function(req, res) {


    User
      .findById(req.payload._id)
      .populate('hero.upgrades')
      .exec(function (err, user) {
        if (err){
          console.log(err);
        } else {
          res.status(200).json(user);
          console.log("success");
        }
      });
    }
};

This is an example of a user

{
    "_id" : ObjectId("57b4b56ea03757e12c94826e"),
    "hash" : "76",
    "salt" : "2",
    "hero" : {
        "upgrades" : [ 
            "57b42773f7cac42a21fb03f9"
        ],
        "total_xp" : 0,
        "unspent_xp" : 0,
        "type" : {
            "heroType" : "Psyker",
            "path" : ""
        },
        "name" : "Jon Doe"
    },
    "username" : "michaelzmyers",
    "faction" : "Grey Knights",
    "email" : "[email protected]",
    "name" : "Michael Myers",
    "roles" : "player",
    "army" : [],
    "armyTotal" : 625,
    "__v" : 3
}

Now, I've tried an array of just the strings with ObjectId's in them, similar to the eample, and I've also tried using ObjectId("STRINGHERE") and no luck. They both return just an empty array. However, if i get rid of the populate call (or change the contents inside populate from hero.upgrades to just hero, or upgrades) then it just returns an array of strings. I feel like the problem is with populate and how I'm using it. HOWEVER, when I had just a single upgrade in my databse (the test upgrade), everything worked fine. Now nothing works. Any thoughts? I'd be happy to provide more code if needed.

Holliholliday answered 17/8, 2016 at 19:45 Comment(0)
A
26

I found that during my little research that it will work:

User
  .findById(req.payload._id)
  .populate({
       path: 'hero.upgrades',
       model: 'Upgrade'
   })
  .exec(function (err, user) {
    if (err){
      console.log(err);
    } else {
      res.status(200).json(user);
      console.log("success");
    }
  });
}

It looks like when user is giving nested object notation i.e. hero.upgrades into populate method, Mongoose got problems with detecting referring model.

Annapolis answered 17/8, 2016 at 20:22 Comment(1)
I suspect this issue is caused by defining schemas across multiples files.Adela

© 2022 - 2024 — McMap. All rights reserved.