[英]i can't populate mongoose deep subdocument
Below is the code that simplified the model and schema I'm having a hard time with下面是简化 model 和架构的代码,我很难使用
const guildSchema = new Schema<Guild>({
sheets: [sheetSchema],
crews: [crewSchema],
});
const GuildModel= getModel('Guild', guildSchema)
const sheetSchema = new Schema<Sheet>({
deales: [dealSchema]
})
const SheetModel = getModel('Guild.sheets', sheetSchema)
const dealSchema = new Schema<Deal>({
crew: [{ type: Schema.Types.ObjectId, refPath: 'Guild.crews' }],
damage: { type: Number, required: true },
})
const DealModel = getModel('Guild.sheets.deales', dealSchema)
const crewSchema = new Schema<Crew>({
name: { type: String, required: true },
})
const CrewModel= getModel('Guild.crews', crewSchema)
and this is Mocha-chai testcode what always throw exception这是 Mocha-chai 测试代码,总是抛出异常
it("populated guild.sheets.deales.boss must have name",async () => {
const guild = await GuildModel.findOne({})
await guild.populate({
path: 'sheets.deales.crew'
}).execPopulate()
expect(guild.sheets[0].deales[0].crew).to.has.property("name") // expected [] to have property 'name'
})
None of the answers on stackoverflow solved my problem. stackoverflow 上的答案都没有解决我的问题。 I wasted 5 hours on just a few lines of this code.
我在这几行代码上浪费了 5 个小时。 please help me
请帮我
You checked this?你检查了这个? https://github.com/Automattic/mongoose/issues/1377#issuecomment-15911192
https://github.com/Automattic/mongoose/issues/1377#issuecomment-15911192
This person changed nested code此人更改了嵌套代码
var opts = {
path: 'author.phone',
select: 'name'
};
BlogPost.populate(docs, opts, function (err, docs) {
assert.ifError(err);
docs.forEach(function (doc) {
console.log(doc);
});
callback(null);
from this由此
var authors = docs.map(function(doc) {
return doc.author;
});
User.populate(authors, {
path: 'phone',
select: 'name'
}, callback);
to this.对此。
author(User)is in BlogPost.作者(用户)在 BlogPost 中。 BlogPost Schema has just User ObjectId, so can't understand author.phone
BlogPost Schema 只有 User ObjectId,所以无法理解 author.phone
I might have already checked it, but I'm uploading it just in case.我可能已经检查过了,但我上传它以防万一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.