我正在尝试feathersjs,我想使用猫鼬连接mongodb并与之交互。我创建了一个简单的模型message.js
,在我的app.js
中需要该模型。但是,当我运行我的应用程序时,出现此错误,然后崩溃:
C:\Users\someuser\development\myapp\node_modules\feathers-mongoose\lib\service.js:17
throw new Error('You must provide a Mongoose Model');
^
Error: You must provide a Mongoose Model
at new Service (C:\Users\someuser\development\myapp\node_modules\feathers-mongoose\lib\service.js:17:13)
at init (C:\Users\someuser\development\myapp\node_modules\feathers-mongoose\lib\service.js:326:10)
at Object.<anonymous> (C:\Users\someuser\development\myapp\app.js:61:22)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
这是我的模型的样子:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var messageSchema = new Schema({
text: String,
author: String,
date: { type: Date, default: Date.now }
});
var Message = mongoose.model('Message', messageSchema);
module.exports = Message;
在我的app.js
中:
const MessageModel = require('./models/message');
我记录了MessageModel中存储的内容并得到:
Model: {"_id":"5b8936062f2e201d1852179e","date":"2018-08-31T12:35:18.499Z"}
我尝试搜索该问题,但是我找不到真正有用的东西,据我所知,我的模型没有任何问题。
答案 0 :(得分:0)
右:您必须像这样注册服务:
const MessageModel = require('./models/message');
app.use('/messages', service({
Model: MessageModel
}));
在示例中,他们将MessageModel称为模型:
const Model = require('./models/message');
他们可以这样做:
app.use('/messages', service({
Model
}));
类似:
app.use('/messages', service({
Model: Model
}));
错误:您可能是这样注册服务的:
app.use('/messages', service({
MessageModel
}));