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

  • SEARCH
  • Home
  • 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 9307947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:30:25+00:00 2026-06-19T00:30:25+00:00

Hi I wondered whether you could help me build a method chain dynamically, I

  • 0

Hi I wondered whether you could help me build a method chain dynamically, I suspect this is easily done if you know how…

Using mongoose and node.js I’m querying a mongo database.
I use a function that I pass an object to which looks like this:

transaction = {
       model: a mongoose model
       req: Express.js request object
       res: Express.js response object
       query: a mongo db query eg, {_id:xxxxx}
       populate: [ {
         field: 'name_of_doc_field'
         select: 'field1,field2'
       } ]
    }

I’m constructing the query dynamically, and in the function I don’t know how many fields I’ll need to populate, or whether an offset or limit is required.

I want to chain my find:

transaction.model.find(transaction.query).skip(offset).limit(limit).exec(function(err, collection_obj){...})

How do i build this chain dynamically, I’ll need to test whether an offset was supplied in the test object, if it was add .skip method to the chain, etc, and with the populate method, there will be an array of items, each item in that array will need a new populate method adding to the chain.

Finally, I’ll need to an exec method to the end of the chain.

Thanks for your help in advance

// Generated by CoffeeScript 1.4.0
    module.exports = {
      findMany: function(transaction) {
        var collection_obj, limit, offset;
        offset = parseInt(transaction.req.query.offset) || 0;
        limit = parseInt(transaction.req.query.limit) || 100;
        collection_obj = null;
        return transaction.model.find(transaction.query).skip(offset).limit(limit).exec(function(err, collection_obj) {
          return found(err, collection_obj, transaction.req, transaction.res);
        });
      },
    }
  • 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-19T00:30:26+00:00Added an answer on June 19, 2026 at 12:30 am

    You can break the chain up into steps that you can conditionally perform based on the contents of transaction:

    var q = transaction.model.find(transaction.query);
    if (offset) {
      q = q.skip(offset);
    }
    if (limit) {
      q = q.limit(limit);
    }
    if (transaction.populate) {
      transaction.populate.forEach(function(pop) {
         q = q.populate(pop.field, pop.select);
      });
    }
    q.exec(function(err, collection_obj){...});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following problem and wondered whether anyone could see why this is
I wonder whether someone may be able to help me please. I'm using this
I am very new to ggplot2 and wondered whether anyone could help me with
I've always wondered about this, and now a client is wondering whether it's feasable:
Wondered if someone could help me write a MySQL query. I noticed in my
I wonder whether someone could possibly help me please. So that I can maintain
I wonder whether someone could help me please. I've really got myself in a
I've been looking at Zookeeper recently and wondered whether anybody was using it currently
I have using .on() in jQuery 1.7 and wondered whether it is possible to
I wonder whether someone can help me please. I've put together this page which

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.