我需要做的是从一个架构方法返回一个特定的“父”值:在猫鼬模式回归“母体”收集特定值
我有两个模式:
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