gpt4 book ai didi

javascript - 如何在 Mongoose 模型中定义方法?

转载 作者:IT老高 更新时间:2023-10-28 11:04:00 26 4
gpt4 key购买 nike

我的 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);
}

关于javascript - 如何在 Mongoose 模型中定义方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com