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

Getting "undefined" for animal size

I can't figure out why I get undefined for the animal size. For example: "Marvin the gray mouse is a undefined animal."

'use strict'
var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/sandbox");

var db = mongoose.connection;

db.on("error", function(err) {
    console.error("connection error:", err);
});

db.once("open", function(){
    console.log("db connection successful");
    //all db comunication goes here
    var Schema = mongoose.Schema;
    var animalSchema = new Schema({
        type:   {type: String, default: "goldfish"},
        color:  {type: String, default: "golden"},
        mass:   {type: Number, default: 0.007},
        name:   {type: String, default: "Angela"}
});

animalSchema.pre("save", function(next) {
    if(this.mass >= 100) {
        this.size = "big";
    } else if (this.mass >= 5 && this.mass < 100) {
        this.size = "medium";
    } else {
        this.size = "small";
    }
    next();
});

var Animal = mongoose.model("Animal", animalSchema);
var Elephant = new Animal({
    type: "Elephant",
    color: "gray",
    mass: 6000,
    name: "Lawrence"
});

var animal = new Animal({}); //saves default

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

var animalData = [
    {
        type: "mouse",
        color: "gray",
        mass: 0.035,
        name: "Marvin"
    },

    {
        type: "nutria",
        color: "brown",
        mass: 6.35,
        name: "Gretchen"
    },

    {
        type: "wolf",
        color: "gray",
        mass: 45,
        name: "Iris"
    },

    Elephant,
    animal,
    whale
];

    Animal.remove({}, function(err) {
        if (err) console.error(err);
        Animal.create(animalData, function(err, animals){
            if (err) console.error(err);
            Animal.find({}, function(err, animals){
                animals.forEach(function(animal){
                    console.log(animal.name + " the " + animal.color +
                    " " + animal.type + " is a " + animal.size + " animal.");
                });
                db.close(function(){
                    console.log("db connection closed");
                });     
            });                         
        });
    });
});

2 Answers

Steven Parker
Steven Parker
243,318 Points

There is no size property in your initial animal data.

The properties you have defined are name, color, type and mass. So it looks like the pre function has not run, or perhaps it only affected the database and the data has not yet been refreshed.

To enable a more complete and accurate analysis, make a snapshot of your workspace and post the link to it here along with a link to the course page you were working with.

Ah, I thought he said to take that out in the video! Thanks!