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
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsCan't log Animal.find() query results.
I can't find a reason why this isn't working, nothing inside the .find() callback works (not even the logging of 'hello') :(
Animal.remove({},(err) => { // remove every Animal document, add query to remove specific ones
if(err) console.error(err);
elephant.save((err) => { // both the collection AND the (elephant) document will be saved now, not before
if(err) console.error(err);
animal.save((err) => { // Inside callback so that all animals are saved before db.close()
if(err) console.error(err);
whale.save((err) => { // Inside callback so that all animals are saved before db.close()
if(err) console.error(err);
Animal.find({size:'big'},(err,animals) => {
animals.forEach(( animal ) => {
console.log(animal.name + ' the ' + animal.color + ' ' +animal.type);
});
console.log('hello');
});
db.close(() => { // Inside callback since .close() is asynchronous
console.log('db connection closed');
});
});
});
});
});
1 Answer
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsI'm sorry, I thought I was in the video's forum section. This is for the Default Values in Mongoose video and I already solved it. It turns out I was closing the db outside of the Animal.find()'s callback, so it closed before it (the callback) could run. The correct code would be:
Animal.find({size:'big'},(err,animals) => {
animals.forEach(( animal ) => {
console.log(animal.name + ' the ' + animal.color + ' ' +animal.type);
});
db.close(() => { // Inside callback since .close() is asynchronous
console.log('db connection closed');
});
});
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsBruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsHere's my full code for reference: