Mongoose populate() returns empty array with no errors

馋奶兔 提交于 2019-12-10 13:48:56

问题


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@gmail.com",
    "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.


回答1:


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.



来源:https://stackoverflow.com/questions/39005072/mongoose-populate-returns-empty-array-with-no-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!