我需要做的是從一個架構方法返回一個特定的「父」值:在貓鼬模式迴歸「母體」收集特定值
我有兩個模式:
var IP_Set = new Schema({
name: String
});
和
var Hash_IP = new Schema({
_ipset : {
type: ObjectId,
ref: 'IP_Set'
},
description : String
});
在Hash_IP
模式我想有以下方法:
Hash_IP.methods.get_parent_name = function get_parent_name() {
return "parent_name";
};
所以當我運行:
var hash_ip = new Hash_IP(i);
console.log(hash_ip.get_parent_name())
我可以得到相關Hash_IP
實例的IP_Set
名稱值。
到目前爲止,我有以下的定義,但我不能設法返回名稱:
Hash_IP.methods.get_parent_name = function get_parent_name() {
this.model('Hash_IP').findOne({ _ipset: this._ipset })
.populate('_ipset', ['name'])
.exec(function (error, doc) {
console.log(doc._ipset.name);
});
};
我已經試過:
Hash_IP.methods.get_parent_name = function get_parent_name() {
this.model('Hash_IP').findOne({ _ipset: this._ipset })
.populate('_ipset', ['name'])
.exec(function (error, doc) {
return doc._ipset.name;
});
};
沒有結果。
在此先感謝您的幫助。
感謝您的獎金答案! :)你在第一個答案中註明的正是我所需要的。我想這樣做,我不需要通過回調,因爲你指向Hash_IP.methods.get_parent_name方法的例子,因爲其他應用程序的要求:(你可以發佈獎勵答案的獎勵例子:D。提前致謝! – diosney 2012-08-02 00:39:37
完成後,看看是否有幫助:) – rdrey 2012-08-02 00:53:30
我差點忘了:+1獎金答案hehehe:D – diosney 2012-08-02 01:00:38