2016-09-16 6 views
0

アロハ!仮想モデルサブ文書内のルートモデルにアクセスする方法はありますか?ここで私のモデルシステムは、私が探しているものを明確にする必要があります。Mongooseを使ってサブ文書内のバーチャルからルートモデル値を取得する

「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

ためのおかげで[OK]を、解決策を見つけました。これは少なくとも2つのレベルをチェックしています:

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); 
}); 
関連する問題