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

时间:2020-12-13 12:58:09

My locationsModel file:

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

To call it, I'm using:

我用:

myLocation.testFunc {locationText: locationText}, (err, results) ->

But I get an error:

但我犯了一个错误:

TypeError: Object function model() {
    Model.apply(this, arguments);
  } has no method 'testFunc'

4 个解决方案

#1


35  

You didn't specify whether you were looking to define class or instance methods. Since others have covered instance methods, here's how you'd define a class/static method:

您没有指定要定义类还是实例方法。由于其他人已经介绍了实例方法,下面是如何定义类/静态方法:

animalSchema.statics.findByName = function (name, cb) {
    this.find({ 
        name: new RegExp(name, 'i') 
    }, cb);
}

#2


23  

Hmm - I think your code should be looking more like this:

嗯,我觉得你的代码应该更像这样:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var threeTaps = require '../modules/threeTaps';


var LocationSchema = new Schema ({
   latitude: String,
   longitude: String,
   locationText: String
});


LocationSchema.methods.testFunc = function testFunc(params, callback) {
  //implementation code goes here
}

mongoose.model('Location', LocationSchema);
module.exports = mongoose.model('Location');

Then your calling code can require the above module and instantiate the model like this:

然后您的调用代码可以要求上述模块,并实例化模型如下:

 var Location = require('model file');
 var aLocation = new Location();

and access your method like this:

像这样访问你的方法:

  aLocation.testFunc(params, function() { //handle callback here });

#3


16  

See the Mongoose docs on methods

参见Mongoose文档中的方法

var animalSchema = new Schema({ name: String, type: String });

animalSchema.methods.findSimilarTypes = function (cb) {
  return this.model('Animal').find({ type: this.type }, cb);
}

#4


0  

Location.methods.testFunc = (callback) ->
  console.log 'in test'

should be

应该是

LocationSchema.methods.testFunc = (callback) ->
  console.log 'in test'

The methods have to be a part of the schema. Not the model.

这些方法必须是模式的一部分。而不是模型。

#1


35  

You didn't specify whether you were looking to define class or instance methods. Since others have covered instance methods, here's how you'd define a class/static method:

您没有指定要定义类还是实例方法。由于其他人已经介绍了实例方法,下面是如何定义类/静态方法:

animalSchema.statics.findByName = function (name, cb) {
    this.find({ 
        name: new RegExp(name, 'i') 
    }, cb);
}

#2


23  

Hmm - I think your code should be looking more like this:

嗯,我觉得你的代码应该更像这样:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var threeTaps = require '../modules/threeTaps';


var LocationSchema = new Schema ({
   latitude: String,
   longitude: String,
   locationText: String
});


LocationSchema.methods.testFunc = function testFunc(params, callback) {
  //implementation code goes here
}

mongoose.model('Location', LocationSchema);
module.exports = mongoose.model('Location');

Then your calling code can require the above module and instantiate the model like this:

然后您的调用代码可以要求上述模块,并实例化模型如下:

 var Location = require('model file');
 var aLocation = new Location();

and access your method like this:

像这样访问你的方法:

  aLocation.testFunc(params, function() { //handle callback here });

#3


16  

See the Mongoose docs on methods

参见Mongoose文档中的方法

var animalSchema = new Schema({ name: String, type: String });

animalSchema.methods.findSimilarTypes = function (cb) {
  return this.model('Animal').find({ type: this.type }, cb);
}

#4


0  

Location.methods.testFunc = (callback) ->
  console.log 'in test'

should be

应该是

LocationSchema.methods.testFunc = (callback) ->
  console.log 'in test'

The methods have to be a part of the schema. Not the model.

这些方法必须是模式的一部分。而不是模型。