我需要的是从 Schema 方法返回一个特定的“父”值:
我有两个架构:
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())
我可以获得关联实例的IP_Set
名称值。Hash_IP
到目前为止,我有以下定义,但我无法返回名称:
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;
});
};
没有结果。
在此先感谢您的帮助。