你好,我是猫鼬的新手,我正试图重新创建将轮班分配给一个人的情况。我已经创建了模式
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);
创建了两个成员,每个成员都被推了一个班。
//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 }
问题是,当我运行以下命令
Member.find({}).populate('shifts').exec(function(err, allMembers){
if(err){
console.log(err)
} else {
console.log(allMembers)
}
})
我从数据库得到响应,但“班次”中有空对象
[ { 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 } ]
我在做什么错?还是我需要遍历每个变量并分别填充?
答案 0 :(得分:0)
那是一个误会。如果您记录一棵更大的树,console.log
不会显示整个树,因为这会令人不知所措,但是只会显示三个嵌套对象深,因此下面的js对象:
{ a: { b: { c: { d: { e: 1 } } } }
显示为:
{ a: { b: { c: [Object] } } }
所以实际上对象不是空的,只是没有显示。