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

203
Visualizações
Api call finds already deleted documents

I've two endpoints: one that delete product document and one that retrieve the document.

After I delete the document throught by Id, the GET api call return me already the document even if it's deleted and It's not present on MongoDb.

Response of DELETE call returns { deletedCount: 1 }

Here code of GET product:

exports.getSingleProduct = (req, res, next) => {
  let id = req.params.id;

  Products.findById(id).populate({ path: 'internalColor' }).then(result => {
    if(result && result.visible == true) {
      res.status(200).json(result)
    } else {
      res.status(404).json({
        message: 'product_not_found',
        id: id
      })
    }
  }).catch(err => {
    res.status(404).json({
      message: 'error_operation: ' + err,
      id: id
    })
  });
}

Here code of DELETE product:

exports.deleteDefinallySingleProduct = (req, res, next) => {
  let id = req.params.id;

  console.log(id)

  Products.deleteOne({ id: id }).then(result => {
    if(result) {
      res.status(200).json({
        message: 'deleted_product'
      });
    }
  }).catch(err => {
    res.status(404).json({
      message: 'error_operation: ' + err
    })
  })
}

Products Model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const options = {
    timestamps: true
}

const productSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    price: {
        type: Number,
        required: true
    },
    externalUrl: {
        type: String,
        required: true
    },
    imgUrl: {
        type: String,
        required: true
    },
    brand: {
        type: String,
        required: true
    },
    visible: {
        type: Boolean,
        required: true
    }
}, options);


const Product = mongoose.model('Products', productSchema);

module.exports = Product;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the error that you are facing is caused by a typo in your code.

exports.deleteDefinallySingleProduct = (req, res, next) => {
 ...

  Products.deleteOne({ id: id }).then(result => {
    if(result) {
      // this code will run always
      console.log(result) // will show { n: 0, ok: 1, deletedCount: 0 },
      // That is, this section of code will run always despite of delete count being 0 or more due to the request to be excecuted successfully.
      ...
}

The correct implementation is here:

exports.deleteDefinallySingleProduct = (req, res, next) => {
 ...

  Products.deleteOne({ _id: id }).then(result => {
   
      ...
}

This is because by default mongooose use _id representing the document id, unless create a custom id in the schema which you didn't do.

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