我的 locationsModel
文件:
mongoose = require 'mongoose'
threeTaps = require '../modules/threeTaps'
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
LocationSchema =
latitude: String
longitude: String
locationText: String
Location = new Schema LocationSchema
Location.methods.testFunc = (callback) ->
console.log 'in test'
mongoose.model('Location', Location);
要调用它,我正在使用:
myLocation.testFunc {locationText: locationText}, (err, results) ->
但我得到一个错误:
TypeError: Object function model() {
Model.apply(this, arguments);
} has no method 'testFunc'
最佳答案
您没有指定是要定义类方法还是实例方法。由于其他人已经涵盖了实例方法,here's如何定义类/静态方法:
animalSchema.statics.findByName = function (name, cb) {
return this.find({
name: new RegExp(name, 'i')
}, cb);
}
https://stackoverflow.com/questions/7419969/