我的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'
4条答案
按热度按时间sf6xfgos1#
你没有指定你是要定义类方法还是示例方法。既然其他人已经介绍了示例方法,下面是你应该如何定义类/静态方法:
brqmpdu12#
嗯-我认为您的代码应该看起来更像这样:
然后,您的调用代码可以要求上述模块,并示例化模型,如下所示:
然后像这样访问你的方法:
ccgok5k53#
请参阅Mongoose方法文档
vpfxa7rd4#
应该是
方法必须是模式的一部分,而不是模型的一部分。