为什么我的 Mongoose 模型无法加载?

发布于 2024-12-05 03:10:24 字数 1045 浏览 1 评论 0原文

这是我的 locationsModel.js 文件:

var LocationSchema, LocationsSchema, ObjectId, Schema, mongoose;
mongoose = require('mongoose');
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
LocationSchema = {
  latitude: String,
  longitude: String,
  locationText: String
};
LocationsSchema = new Schema(LocationSchema);
LocationsSchema.method({
  getLocation: function(callback) {
    return console.log('hi');
  }
});
exports.Locations = mongoose.model('Locations', LocationsSchema, 'locations');

在我的控制器中,我有:

var Locations, mongoose;
mongoose = require('mongoose');
Locations = require('../models/locationsModel').Locations;
exports.search = function(req, res) {
  var itemText, locationText;
  Locations.getLocation('info', function(err, callback) {
    return console.log('calleback');
  });
  return;
};

当我运行它时,我收到以下错误:

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

我缺少什么?

This is my locationsModel.js file:

var LocationSchema, LocationsSchema, ObjectId, Schema, mongoose;
mongoose = require('mongoose');
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
LocationSchema = {
  latitude: String,
  longitude: String,
  locationText: String
};
LocationsSchema = new Schema(LocationSchema);
LocationsSchema.method({
  getLocation: function(callback) {
    return console.log('hi');
  }
});
exports.Locations = mongoose.model('Locations', LocationsSchema, 'locations');

In my controller, I have:

var Locations, mongoose;
mongoose = require('mongoose');
Locations = require('../models/locationsModel').Locations;
exports.search = function(req, res) {
  var itemText, locationText;
  Locations.getLocation('info', function(err, callback) {
    return console.log('calleback');
  });
  return;
};

When I run it, I get the following error:

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

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2024-12-12 03:10:24

我认为你追求的是静态而不是方法。

根据 docs

我认为你应该定义 getLocations函数如下(查看您对 getLocations 的使用,您有一个字符串参数以及回调:

LocationsSchema.statics.getLocation = function(param, callback) {
    return console.log('hi');
}

编辑:

staticsmethods是你是否是在该类型的“类型”或“对象”上调用它改编自 examples

BlogPostSchema.methods.findCreator = function (callback) {
  return this.db.model('Person').findById(this.creator, callback);
}

您可以这样调用:

BlogPost.findById(myId, function (err, post) {
  if (!err) {
    post.findCreator(function(err, person) {
      // do something with the creator
    }
  }
});

I think what you're after is statics rather than a method.

As per the docs:

I think you should define the getLocations function as follows (looking at your use of getLocations you've got a string parameter as well as the callback:

LocationsSchema.statics.getLocation = function(param, callback) {
    return console.log('hi');
}

EDIT:

The difference between statics and methods is whether you are calling it on the "type" or "objects" of that type. Adapted from the examples:

BlogPostSchema.methods.findCreator = function (callback) {
  return this.db.model('Person').findById(this.creator, callback);
}

which you'd invoke as such:

BlogPost.findById(myId, function (err, post) {
  if (!err) {
    post.findCreator(function(err, person) {
      // do something with the creator
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文