What I need is to return a specific “parent” value from a Schema method:
I have two Schemas:
var IP_Set = new Schema({
name: String
});
and
var Hash_IP = new Schema({
_ipset : {
type: ObjectId,
ref: 'IP_Set'
},
description : String
});
In the Hash_IP schema I would like to have the following method:
Hash_IP.methods.get_parent_name = function get_parent_name() {
return "parent_name";
};
so when I run:
var hash_ip = new Hash_IP(i);
console.log(hash_ip.get_parent_name())
I can get the IP_Set name value of the associated Hash_IP instance.
So far I have the following definition, but I can’t manage to return the 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) {
console.log(doc._ipset.name);
});
};
I’ve tried:
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;
});
};
without results.
Thanks in advance for the help.
I believe that you’re very close. Your question isn’t very clear on this, but I assume that
is working and
is not?
Unfortunately the async
returnis not working the way you want it to..execcalls your callback function, which returns the name. This does not return the name as the return value forget_parent_name(), though. That would be nice. (Imagine thereturn return namesyntax.)Pass in a callback into
get_parent_name()like this:You can now use
instance_of_hash_ip.get_parent_name(function (err, doc) { ... do something with the doc._ipset.name ... });in your code.Bonus answer 😉
If you use your parent’s name a lot, you might want to always return it with your initial query. If you put the
.populate(_ipset, ['name'])into your query for the instance of Hash_IP, then you won’t have to deal with two layers of callbacks in your code.Simply put the
find()orfindOne(), followed bypopulate()into a nice static method of your model.Bonus example of bonus answer 🙂
Each model instance now has models._ipset.name already defined. Enjoy 🙂