0

I am working on a React frontend with a delete method which deletes an item in the database. Having a hard time with the following code.

deleteFromDB = idTodelete => {
    let objIdToDelete = null;
    this.state.data.forEach(dat => {
      if (dat.id == idTodelete) {
        objIdToDelete = dat._id;
      }
    });

This is the method called after entering the ID to be deleted and this modifies the state by deleting the item corresponding to the ID

shiva addanki
  • 85
  • 1
  • 10
  • This could be a typo, but maybe the objects in your state have two `ID` attributes. `id` might be a 'frontend' ID whereas `_id` is the ID used in the database. – alex kucksdorf May 10 '19 at 09:42
  • Possible duplicate of [What is the underscore "\_" in JavaScript?](https://stackoverflow.com/questions/44734399/what-is-the-underscore-in-javascript) – Quentin May 10 '19 at 09:56

2 Answers2

3

It's a naming convention for private variables and methods used by some developers to indicate that they are private.

Also see: What is the underscore "_" in JavaScript?

Kelvin
  • 320
  • 4
  • 19
1

It may be possible that _id is actually the primary key of the data you want to delete, as it is actually used in mongodb

Raj Saraogi
  • 1,780
  • 1
  • 13
  • 21