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 trialLucas Santos
19,315 Pointsseed.js code not clear, I have a question
seed.js
'use strict';
var Todo = require('./models/todo.js');
var todos = [
'Walk Dog',
'Take out Trash',
'Study'
];
todos.forEach(function(todo, index){
Todo.find({'name': todo}, function(err, todos){
if(!err && !todos.length){
Todo.create({completed: false, name: todo});
}
});
});
I have couple of questions that were not clarified in this video that I wanted to be 100% sure of and not 99.9%.
The forEach method used on the todo is the one from AngularJs correct?
The second parameter of the callback function in the find method Huston uses the variable todos which in my opinion is really bad to do because it can be extremely confusing with the todos array on top. The second argument of the callback after the err argument is the result of the find method within that foreach loop meaning it is returning one todo at a time and not multiple "todos" so using the plural of todo is also really confusing as it is only returning one at a time. So I guess the question here would be. . . am I wrong about this? That todos second parameter was very confusing even for someone who understands MEAN. Should of just named it todoResult
And last but not least is in the if statement he used !todos.length to check if the todo exists. I have never seen someone use that logic to check if an array exists. Can someone elaborate on that little piece of logic and what he is trying to accomplish because from what I understood he is trying to check if the result of the find method does not exist.
if(!err && !todos.length)
Thank you.
2 Answers
Jaro Schwab
8,957 PointsHey.
Yep, i think so.
Yeah, in this Situation, i might be better to name it todoResult or something like this. Just note: the find method could return multiple documents. For example, if you had a field called Category and you would search after this, it would return all the documents within the given parameter. So you are wrong about thinking it returns only one document, because you are in the forEach loop - it returns only one document, because there is just one Todo with each Name..
From what i know, this is pretty common is Javascript. You are right, he is just checking if the Todo already exists. obviously, if the todo doesn't exists, the result of "todos.length" is 0..
Ken Howard
Treehouse Guest TeacherHey Lucas Santos ...
1) We are leveraging the Array.prototype.forEach
method in this example. You can find documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
2) Todo.find returns an array. We are searching the entire collection of Todos stored in MongoDB and the result will always be an array of Todos. So the naming here, while it conflicts with the seed array's variable name, is still accurate since the response is an array.
3) Because we know the response will be an array (if not an error) we just need to check if the length is falsy. The problem we are solving is just avoiding duplicate seed data in the database.
I hope this clarifies things a bit.
Lucas Santos
19,315 PointsHey Ken thanks for the reply. I understand now, mongooses methods were throwing off my logic. I see if the collection has nothing in it the the seed data is injected and creates the record.
Thanks Ken appreciate it!
Anthony c
20,907 Points- Is there a reason he used create instead of Todo.insert?
- This is seeding the actual mongo database correct?
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsThank you!
Jonathan Cousins
7,508 PointsJonathan Cousins
7,508 Points.forEach
a standard method of theArray.prototype
?