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

211
Views
Problems in deleting an item in ReactJS and MongoDB

I am building an inventory project using ReactJS, ExpressJS and MongoDB, which is basically an CRUD project. My project working perfectly for all operations except the Delete. When I want to delete an item, my project can delete an item but it always deletes the first item of the list. Whenever I tried to delete other items except First Item it always deletes the first item

Here I am giving my Client Side Code:

const handleDelete = (id) => {
    const url = `http://localhost:5000/inventory/${id}`;
    fetch(url, {
      method: "DELETE",
    })
      .then((res) => res.json())
      .then((data) => {
        const remaining = products.filter((product) => product._id !== id);
        setProducts(remaining);
      });
  };

I have sent this function as a props in my SingleItem Component and added it as an eventHandler

Here I am giving eventHandler Code:

<button onClick={() => handleDelete(_id)} type="button">
   Delete
</button>

Now I'm giving my Server side code:

app.delete("/inventory/:id", async (req, res) => {
      const id = req.params.id;
      const query = { _id: ObjectId(id) };
      const result = await productCollection.deleteOne(query);
      res.send(result);
});

over 4 years ago · Santiago Trujillo
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!