Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda