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

118
Views
GetByUserID as a callback function (express)

A callback function is passed as an argument to another function. In order to respect the separation of concerns, how can I rewrite the following example as a callback? I know that the Req,Res part is outsourced to the router.

Userservice.js

function getByUserId(req, res, next) {

  let userIDD = req.body.userID;
  User.findOne({
    userID: userIDD
  }, function(err, result) {
    if (err) {
      console.log("error: " + err)
    } else {

      console.log("Awesome.");
      res.send(result)
    }
  })
}

UserRoute.js

router.post('/publicUser/getByUserID', userService.getByUserId)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think it is good now but if you want to use callback and use this function as a middleware you can do next(result) instead of res.send(result)

about 4 years ago · Juan Pablo Isaza Report

0

If you want to declare a callback function directly in your post method you can use an arrow function:

router.post('/publicUser/getByUserID', (req, res) => {
  let userIDD = req.body.userID;
  User.findOne({
    userID: userIDD
  }, function(err, result) {
    if (err) {
      console.log("error: " + err)
      return res.send(err);
    } 

    console.log("Awesome.");
    res.send(result)
  })
})
about 4 years ago · Juan Pablo Isaza 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!