Saturday 1 April 2017

Mongoose Model Methods: Properties don't save?

I'm trying to write a method on a mongoose model that will populate some fields based on an object I pass in.

let mySchema = mongoose.Schema({
    name: String,
    age: Number,
    street: { type: String, default: 'No' }
});

mySchema.methods.populate = function(o) {
    this.age = o.age + 10;
});

Elsewhere, i'll initialize an instance and run the method:

let newThing = new MySchema();
newThing.populate({ age: 12 });
newThing.save();

This successfully saves a new object in mongo with no properties other than the default street name. Am I doing something wrong?



via opticon

No comments:

Post a Comment