Mongoose: How to access properties of instance in an instance method

时间:2016-08-31 12:11:03

标签: node.js mongodb mongoose

I have this schema:

var Place = mongoose.Schema({
    lat: Number,
    lng: Number,
    icon: String,
    name: String,
    place_id: String,
    types: [String],
    vicinity: String
});

Place.methods.isInsideRadius = (centerLocation, radius) => {
    console.log(radius);
    console.log(centerLocation);
    console.log(this);
    return true; 
}

How can I access the properties of the instance that is calling this method? I tried printing the this but it returns an empty object.

1 个答案:

答案 0 :(得分:5)

Ah my mistake was I was using the fat arrow function. When I used the function() it already worked.