javascript - 我无法填充猫鼬深层子文档
问题描述
下面是简化我遇到困难的模型和模式的代码
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)
这是 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'
})
stackoverflow 上的答案都没有解决我的问题。我在这几行代码上浪费了 5 个小时。请帮我
解决方案
你检查了这个?https://github.com/Automattic/mongoose/issues/1377#issuecomment-15911192
此人更改了嵌套代码
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);
由此
var authors = docs.map(function(doc) {
return doc.author;
});
User.populate(authors, {
path: 'phone',
select: 'name'
}, callback);
对此。
作者(用户)在 BlogPost 中。BlogPost Schema 只有 User ObjectId,所以无法理解 author.phone
我可能已经检查过了,但我上传它以防万一。
推荐阅读
- java - GridLayout 和 CardView 无法在所有设备上完美运行
- node.js - 在 Heroku 上,如何使 boost 库可用于编译 NodeJS 应用程序?
- javascript - 格式化字段(详尽 & ES3)
- oracle - 我如何在 oracle apex 中将负数变为红色
- ruby - 在 META-INF 中找不到签名文件。无法继续。(运行时错误)
- css - 使用媒体断点复制 Bootstrap flexbox 网格系统
- python-3.x - 使用 python 脚本在网页上执行“ctrl+F”...
- laravel - 将输入值传递给 v-model
- autocomplete - PHP、JS 和 CSS 自动完成在 vs 代码中很糟糕……为什么?
- python-3.6 - PubNub Tornado 对象初始化引发异常