【发布时间】:2017-07-20 19:52:32
【问题描述】:
我尝试在我的 Nodejs 项目中使用 feathersjs 进行猫鼬查询。我想在服务类中使用猫鼬查询,就像其他常用服务一样:查找、获取、更新等。
下面是我的模式模型:
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
const messageModel = mongoose.model('message', schema);
module.exports = messageModel;
我知道我可以使用这些表格:
app.use('/messages/:id', service(options), function(req, res){
message.find({ name: req.params.id }, function (err, result) {
if (err) return err;
res.json(result);
})
});
但对我来说最好使用服务类并以这种方式使用猫鼬查询:
'use strict';
const service = require('feathers-mongoose');
const message = require('./message-model');
const hooks = require('./hooks');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
class Service {
constructor(options) {
this.options = options || {};
}
find(params) {
message.find({}, function (err, result) {
if (err) return err;
return Promise.resolve([]);
})
}
get(id, params) {
message.find({ name: id }, function (err, result) {
if (err) return err;
return Promise.resolve([]);
})
}
}
module.exports = function() {
const app = this;
const options = {
Model: message,
paginate: {
default: 5,
max: 25
}
};
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/feathers');
app.use('/messages', new Service(options));
// Get our initialize service to that we can bind hooks
const messageService = app.service('/messages');
// Set up our before hooks
messageService.before(hooks.before);
// Set up our after hooks
messageService.after(hooks.after);
};
module.exports.service = Service
出了点问题,因为它无法工作。我该怎么做?
【问题讨论】:
标签: node.js mongodb rest mongoose feathersjs