0
我尝试在使用feathersjs的我的Nodejs项目中执行猫鼬查询。我想用在像其他服务类猫鼬查询像常用服务:发现,获取,更新等如何在使用feathersjs的Service类中使用查询猫鼬
下面是我的架构模型:
'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
有些东西是错的,因为它无法正常工作。我该怎么做?
谢谢!它工作得很好。我还有一个问题。如果需要一个选项?我尝试将对象指向构造函数,但在方法中没有定义选项。例如:'console.log(options)' – SeaDog
我不确定你的意思。如果你不需要,你不必添加'构造函数'(或其他任何方法)。 – Daff
我的意思是在默认生成的服务中使用feathersjs和mongodb是一个常量'const options = {model:model,default:5,等等}'这个常量以这种方式直接服务于:'app.use('/ messages ',service(options),function(req,res){...});'在我的情况下,我需要直接将这个选项传递给我的Service类的构造函数,这个类的默认方法是:'app.use('/消息',新的服务(选项));'但如果我的收藏有超过5个文件,它们全部显示。如何使用常量选项只显示5个文档? – SeaDog