Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:29:16+00:00 2026-06-08T23:29:16+00:00

What I need is to return a specific parent value from a Schema method:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:29:18+00:00Added an answer on June 8, 2026 at 11:29 pm

    I believe that you’re very close. Your question isn’t very clear on this, but I assume that

    .populate('_ipset', ['name'])
    .exec(function (error, doc) {
      console.log(doc._ipset.name);
    });
    

    is working and

    .populate('_ipset', ['name'])
    .exec(function (error, doc) {
       return doc._ipset.name;
    });
    

    is not?

    Unfortunately the async return is not working the way you want it to.

    .exec calls your callback function, which returns the name. This does not return the name as the return value for get_parent_name(), though. That would be nice. (Imagine the return return name syntax.)

    Pass in a callback into get_parent_name() like this:

    Hash_IP.methods.get_parent_name = function get_parent_name(callback) {
        this.model('Hash_IP').findOne({ _ipset: this._ipset })
            .populate('_ipset', ['name'])
            .exec(callback);
    };
    

    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() or findOne(), followed by populate() into a nice static method of your model.

    Bonus example of bonus answer 🙂

    var Hash_IP = new Schema({
        _ipset      : {
            type: ObjectId,
            ref: 'IP_Set'
        },
        description : String
    });
    
    Hash_IP.statics.findByDescWithIPSetName = function (desc, callback) {
        return this.where('description', new RegExp(desc, 'i'))
            .populate('_ipset', ['name']) //Magic built in
            .exec(cb)
    };
    
    module.exports = HashIpModel = mongoose.model('HashIp', Hash_IP);
    
    // Now you can use this static method anywhere to find a model 
    // and have the name populated already:
    
    HashIp = mongoose.model('HashIp'); //or require the model you've exported above
    HashIp.findByDescWithIPSetName('some keyword', function(err, models) {
       res.locals.hashIps = models; //models is an array, available in your templates
    });
    

    Each model instance now has models._ipset.name already defined. Enjoy 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to return a specific number of rows from a query
I need to return a full directory name from inside a specified directory that
I need to return two values at a time, so I have: class IterableObject(object):
I have need to return multiple results from a subquery and have been unable
I have an abstract parent class(Product) and two child classes that inherit from it(Book,Software).
i need to return a JSON with this format: {answers: [{id: 93, value:Ahstron}, {id=94,
I need to return server time from a webservice. I've got the following code:
I need to return multiple values from a ColdFusion function in an ajax callback
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
Following on from another question I have asked I have created a new class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.