猫鼬populate()返回没有错误的空数组 [英] Mongoose populate() returns empty array with no errors

查看:117
本文介绍了猫鼬populate()返回没有错误的空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使这些填充的东西起作用,但是我遇到了问题,因为我没有得到预期的结果,并且没有错误可以使用.只是一个空数组.

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 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);

和另一个

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' }
});

我正在尝试这样做

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");
        }
      });
    }
};

这是一个用户示例

{
    "_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
}

现在,我尝试了一个仅包含对象ID的字符串组成的数组,类似于示例,并且我也尝试了使用ObjectId("STRINGHERE")并且没有运气.它们都只返回一个空数组.但是,如果我摆脱了填充调用(或将填充中的内容从hero.upgrades更改为hero或升级),则它只会返回一个字符串数组.我觉得问题出在人口以及使用方式上.但是,当我在数据库中仅进行一次升级(测试升级)时,一切正常.现在什么都行不通了.有什么想法吗?如果需要,我很乐意提供更多代码.

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.

推荐答案

我发现,在我的小小的研究中,它会起作用:

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");
    }
  });
}

当用户在populate方法中给嵌套对象表示法(即hero.upgrades)时,Mongoose遇到了检测引用模型的问题.

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

这篇关于猫鼬populate()返回没有错误的空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆