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

Can'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

I'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');
    });
});

Here's my full code for reference:

'use strict';

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/sandbox');

var db = mongoose.connection;

db.once('open',function(){
    console.log('Connection successful');
    // All db communication

    var Schema = mongoose.Schema;
    var AnimalSchema = new Schema({
        type:   {type: String, default: 'magikarp'}, // default values
        size:   String,
        color:  {type: String, default: 'orange'}, // default values
        mass:   {type: Number, default: 10}, // default values
        name:   {type: String, default: 'Shinji'} // default values
    });

        var Animal = mongoose.model('Animal',AnimalSchema); // Mongo will make Animal plural

    var elephant = new Animal({
        type: 'elephant',
        color: 'gray',
        mass: 6000,
                size:'big',
        name: 'Lawrence'
    });

    var whale = new Animal({
        type: 'whale',
        mass: 190500,
                size:'big',
        name: 'Fig'
    });

    var animal = new Animal({}); // defualt Magikarp value

        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');
                        });
                    });
                });
            });
        });
    });
});

/* Will log:
    Connection successful
    Lawrence the gray elephant
    Fig the orange whale
    hello
    db connection closed
*/