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

138
Views
Get single document in express doesn't jumps to catch

I have the following express code. If I try to get a document with a non-existent id, I get nothing. The problem is that I get 200 as response status and I don't get the Failed message. The code jumps every time into "then".

const Mymodel = require('../model/Mymodel');

const Single = (req, res) => {
  const id = req.params.id;
  Mymodel.findById(id)
    .then(result => {
      res.send(result);
    })
    .catch(err => {
      console.log(err);
      res.status(404).send({"Failed": "Document not found"});
    });
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

your finding query response is null so this is not an error. if you send res.status(404).send({"Failed": "Document not found"}); response for not found use this.

const Mymodel = require('../model/Mymodel');

const Single = (req, res) => {
  const id = req.params.id;
  Mymodel.findById(id)
    .then(result => {
      if(result){
       res.send(result);
      }else{
       res.status(404).send({"Failed": "Document not found"});
      }
    })
    .catch(err => {
      console.log(err);
      res.status(404).send({"Failed": "Document not found"});
    });
}
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!