简体   繁体   English

猫鼬在填充所有对象时返回空对象?

[英]Mongoose returning empty objects when populating all?

Hello I'm new to mongoose and I was trying to recreate a situation where a shift is assigned to a person. 您好,我是猫鼬的新手,我正在尝试重新创建将轮班分配给某人的情况。 I've create the schemas 我已经创建了架构

    var shiftSchema = new mongoose.Schema({
    date: Date,
    location: String,
    job: String,
    event: String,
    calltime: Number,
    hours: Number
});

var Shift = mongoose.model("Shift", shiftSchema);

var memberSchema = new mongoose.Schema({
    id: Number,
    secondaryid: Number,
    firstname: String,
    lastname: String,
    profile: String,
    active: Boolean,
    shifts: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Shift"
    }]
});

var Member = mongoose.model("Member", memberSchema);

and created two members and each one was pushed a shift. 并创建了两个成员,每个成员都被转移了一个班次。

//Members in DB
{ "_id" : ObjectId("5bb14deb574f2b0d627c7ae2"), "shifts" : [ ObjectId("5bb14deb574f2b0d627c7ae3") ], "id" : 1000001, "secondaryid" : 20000001, "firstname" : "Jane", "lastname" : "Doe", "profile" : "Admin", "active" : true, "__v" : 1 }
{ "_id" : ObjectId("5bb14e13f445e50d7ba0257d"), "shifts" : [ ObjectId("5bb14e9f19e7670d91478323") ], "id" : 1000000, "secondaryid" : 20000000, "firstname" : "John", "lastname" : "Doe", "profile" : "Admin", "active" : true, "__v" : 1 }

//Shifts in DB
{ "_id" : ObjectId("5bb14deb574f2b0d627c7ae3"), "date" : ISODate("1970-01-01T00:00:00Z"), "location" : "Office2", "job" : "Clerk2", "event" : "", "calltime" : 7, "hours" : 7, "__v" : 0 }
{ "_id" : ObjectId("5bb14e9f19e7670d91478323"), "date" : ISODate("1970-01-01T00:00:00Z"), "location" : "Office", "job" : "Clerk", "event" : "", "calltime" : 8, "hours" : 8, "__v" : 0 }

The problem is that when I run the following 问题是当我运行以下命令时

Member.find({}).populate('shifts').exec(function(err, allMembers){
    if(err){
        console.log(err)
    } else {
        console.log(allMembers)
    }
})

I get a response from DB but "shifts" has empty objects 我从数据库得到响应,但“班次”中有空对象

[ { shifts: [ [Object] ],
    _id: 5bb14deb574f2b0d627c7ae2,
    id: 1000001,
    secondaryid: 20000001,
    firstname: 'Jane',
    lastname: 'Doe',
    profile: 'Admin',
    active: true,
    __v: 1 },
  { shifts: [ [Object] ],
    _id: 5bb14e13f445e50d7ba0257d,
    id: 1000000,
    secondaryid: 20000000,
    firstname: 'John',
    lastname: 'Doe',
    profile: 'Admin',
    active: true,
    __v: 1 } ]

What am I doing wrong? 我究竟做错了什么? or do I need to loop through each and populate individually? 还是我需要遍历每个变量并分别填充?

Thats a missunderstanding. 那是个误会。 If you log a bigger tree, console.log doesn't show the whole tree cause that would be overwhelming but instead it only shows three nested objects deep, so the following js object: 如果您记录一棵更大的树, console.log不会显示整个树,因为这会令人不知所措,但只会显示三个嵌套对象深,因此以下js对象:

 { a: { b: { c: { d: { e: 1 } } } }

gets displayed as: 显示为:

 { a: { b: { c: [Object] } } }

So actually the object isn't empty, its just not shown. 所以实际上对象不是空的,只是没有显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM