Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Input field function when aggregating data in node and mongoDB

I am trying to develop a data aggregation feature for an application. The user should be able to enter a name and it'll search the database collection for that name and return the collection. I am trying to implement it using node.js but i am confused as to how to pass a parameter for the name.

Controller:

exports.DAFacility_author_search = (req, res) => {
  DAFacility.aggregate([
    [
      {
        '$match': {
          'author': author
        }
      }
    ]
  ]).then((DAFacility) => {
    res.send(DAFacility);
  })
  .catch((e) => {
    res.send(e);
  });
};

Route:

router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search)

For the 'author': author i am trying to use author as the variable name but i dont exactly know how to structure the controller and router to take in a parameter so that i can retrieve a value on postman. Any help would be great Thank you

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I suppose that your request in postman will be something like:

{
    "author": "foo",
    ...
}

When your request is received by the API, the body is available in your req object in the controller, so, if you want to use author field in your aggregate, you simply access: req.body.author.

Your controller could have the next structure:

export const DAFacilityController = {
    DAFacility_author_search: (req, res) => {
        DAFacility.aggregate([
            {'$match': {'author': req.body.author}}
        ])
    .then((DAFacility) => {
        res.send(DAFacility);
    }).catch((e) => {
        res.send(e);
    });
    },

    DAFacility_another_method: (req, res) => {...},
}

The router is ok:

router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search);
router.get('/DAFacilityAuthor/<something>', DAFacilityController.DAFacility_another_method);

I hope I helped

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!