阿罗哈!有没有办法从虚拟 子文档中访问根模型?这是我的模型系统,应该清楚我正在寻找什么。
事实上,“ImageSchema”将被包含在Root-Model中的几个地方,与Root-Model有不同的“路径”。 “parent()”有时会起作用,但不会起作用。
var ImageSchema = new Schema({
name: String
})
ImageSchema.virtual('url').get(function () {
var parent = this.parent(); // Works
var root = this.root(); // Anything like this available?
return '/'+root.category+'/'+this.name;
});
var CombinationSchema = new Schema({
name: String,
description: String,
images: [ImageSchema]
})
var RootSchema = new Schema({
name: String,
category: String,
description: String,
combinations: [CombinationSchema]
})
感谢
答案 0 :(得分:0)
好的,找到了解决方案。这至少要检查两个级别:
ImageSchema.virtual('url').get(function () {
var parent = this.parent()
var root = (parent.parent ? parent.parent() : parent)
console.log(this.parent().description +" "+ root.category + " " + this.name);
});