【问题标题】:How to use a query mongoose in Service class with feathersjs如何使用 Feathersjs 在服务类中使用查询猫鼬
【发布时间】: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


    【解决方案1】:

    您正在回调中返回一个承诺。相反,使用回调的异步调用必须包装在 new Promise((resolve, reject) => {}) 中。

    find(params) {
      return new Promise((resolve, reject) => {
        message.find({}, function (err, result) {
         if (err) {
          return reject(err);
         }
         return resolve(result);
        });
      });
    }
    

    Bluebird 等工具可以更轻松地将基于回调的 API 自动转换为 Promise。

    【讨论】:

    • 谢谢!它工作得很好。我还有一个问题。如果需要选项?我尝试将对象定向到构造函数,但在方法中未定义选项。例如:console.log (options)
    • 我不确定你的意思。如果您不需要它,则不必添加 constructor(或任何其他方法)。
    • 我的意思是默认情况下使用feathersjs生成的服务和mongodb是一个常量const options = { Model: model, default: 5, etc. }这个常量直接以这种方式服务:app.use('/messages', service(options), function(req, res){ ... });在我的情况下,我需要将此选项直接用于我的构造函数服务类调用它们的默认方法相同:app.use('/messages', new Service(options)); 但如果我的集合有超过 5 个文档,它们会全部显示。如何使用常量选项只显示 5 个文档?
    • 如果您要实现自己的服务,则必须自己完成。您可以在github.com/feathersjs/feathers-mongoose/blob/master/src/… 中查看现有适配器是如何实现的
    • 好的,目前我的项目中不需要任何选项,但将来我可能会需要它。我要保存你的链接,谢谢!
    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2020-03-06
    • 2018-12-08
    相关资源
    最近更新 更多