Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda