为什么我的 Mongoose 模型无法加载?
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你追求的是静态而不是方法。
根据 docs:
我认为你应该定义
getLocations
函数如下(查看您对getLocations
的使用,您有一个字符串参数以及回调:编辑:
statics
和methods是你是否是在该类型的“类型”或“对象”上调用它改编自 examples:
您可以这样调用:
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 ofgetLocations
you've got a string parameter as well as the callback:EDIT:
The difference between
statics
andmethods
is whether you are calling it on the "type" or "objects" of that type. Adapted from the examples:which you'd invoke as such: