Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Building a MEAN Application Creating and Editing Data in a MEAN App Editing Data with PUT Routes in Express

Deano k
PLUS
Deano k
Courses Plus Student 5,872 Points

why use _id?

What the reason behind using _id notation in this comparison (todo && todo._id !== id)? why not just todo.id?

2 Answers

Iulia Lungu
Iulia Lungu
1,562 Points
{
        "_id": "572b1ebbf46dfa24109e508f",
      "completed": true,
      "name": "this is the EDITED"

    }

The "_id" comes from the '_id' key assigned by MongoDB to each of the documents in a collection. You should first take the Mongo Basics course here on Treehouse.

Anthony c
Anthony c
20,907 Points

When you click edit/save angular will put the _id of the clicked todo into the request's url.

This conditional is a safety check that happens before the new data (new todo) is sent to overwrite the old data (old todo). It basically is there to make sure that the todo you are overwriting/editing is the correct one... in other words, the id from the url's param is the same as the _id of the todo you are about to edit.

just to clarify, the "id" is referring to a variable called "id" which is storing the number/id found in the URL param. The todos._id is the unique _id in mongo. So again you're comparing the variable id with the _id. If they match, you can safely update the database todo with the given _id. If they don't match, then you have made sure you don't update.