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

193
Views
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 answers
Answer question

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 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!