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

242
Views
React - using express router, mongoose, which approach is better?

Giving by example the GET route request below, both WORK. I am trying to understand their differences and which one is the best approach for doing this. Any comments on the 2 coding options, please?

Option 1

router.get("/", (req, res) => {
   AddStudents.find({})
     .then((data) => {
       res.json(data);
     })
     .catch((error) => {
     });
 });

Option 2

router.get("/", async (req, res) => {
  AddStudents.find({}, (err, result) => {
    if (err) {
      res.send(err);
    }
    res.json(result);
  });
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

res.json will convert the data to json format. So it will convert the data to

res.json: content-type: application/json

from

res.send: content-type: text/html

option 2

async 

will automatically wrap the data in a promise so you dont have to do that.

So when an array is passed they return the same but if there are null or undefined values in the data the res.json will do explicit JSON conversion of them cuz aint valid json

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!