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

144
Views
Why I still get the 200 response status code instead of 404?

I just making delete query to mysql with sequelize.

const model = require('../../../config/model/index');
const controller = {};

controller.drop =  async function(req, res) {
  try {
    await model.activitys.destroy({
      where: {
        id: req.params.id
      }
    })
    check_id = model.activitys.findAll({
      where: {
        id: req.params.id
      }
    })
    if(check_id!=null){
      res.status(200).json({
        status: "Success",
        message: "Success",
        data: {}
      })
    }else{    
      res.status(404).json({
        status: "Not Found",
        message: `Activity with ID ${id} Not Found`,
        data: {}
      })    
    }
  } catch (error) {
    res.status(404).json({
      status: "Error",
      message: error.message
    })
  }
}

module.exports = controller;

I want to delete the data on DB from id parameters, it's work for deleting the data. But when I try to delete by id that not exist in my DB, it's still get the 200 status code.

How to make that will return 404 status code if there's no data exists in DB ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to check if a single record exists in DB then use findOne or findByPk instead of findAll. findAll always returns an array (either empty or not) and that means it's always not equal to null:

check_id = model.activitys.findOne({
      where: {
        id: req.params.id
      }
    })
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!