如何在FeathersJS服务扩展中使用app.service('myService')?

问题描述 投票:0回答:2

我正在尝试在FeathersJS应用程序中为名为find()的服务扩展properties方法。

我需要在find()返回的所有记录中追加一个数组,该数组具有来自另一个名为propertyadds的服务的整数。这意味着我需要从

更改响应
{
  "total": 2973,
  "limit": 10,
  "skip": 0,
  "data": [
    {
      "id": 1,
      ...
    },
    {
      "id": 2,
      ...
    }
    ...
  ]
}

{
  "total": 2973,
  "limit": 10,
  "skip": 0,
  data: [
    {
      "id": 1,
      ...
      "additionals": [10,20,30]
    },
    {
      "id": 2,
      ...
      "additionals": [12,25,33]
    }
    ...
  ]
}

附加项中的值来自服务propertyadds,该服务管理类似的表

| property_id | additional_id |
|-------------|---------------|
|      1      |       10      |
|      1      |       20      |
|      1      |       30      |
|      2      |       12      |
|      2      |       25      |
|      2      |       33      |
|-------------|---------------|

我最初的想法是像这样扩展properties服务

const { Service } = require('feathers-sequelize');

exports.Properties = class Properties extends Service {
  async find(data,params) {
      let newResponse = await super.find(data,params);
      let newData = newResponse.data.map(async pr => {
          pr.additionals = await app.service('propertyadds').find({
              properti_id: pr.id
          })
          return pr;
      })
      return {
          total: newResponse.total,
          limit: newResponse.limit,
          skip: newResponse.skip,
          data: newData
      }
  }
};

问题是我在app中没有src/services/properties/properties.class.js,并且(我是FeathersJS的新手,但我不知道如何获得它。

我需要一个有效的app常量来访问此模块内的所有服务,该怎么办?

javascript feathersjs
2个回答
0
投票

我不知道是用羽毛来代替,但是根据doc

对于调用app.listen之前注册的服务,在调用app.listen ...时将调用每个注册服务的设置功能。

通过从Service继承(我不知道这是否是标准方法),也许Service实现了setup,并且您可以从this.app内部访问Properties::find()

如果没有,请写下来:

async find(data,params) {
...
}
async setup(app, path){
  await super.setup && super.setup(app, path)
  // for now no setup on Service
  // should Service adds a setup, it is unlikely that it wraps app to a 
  // whole new object and assigns it to this, so we can still
  // assign app to this with a relative confidence
  this.app = app
}

事实上,当我访问src/services/properties.service.js并找到此行时,解决方案就实现了>

app.use('/properties', new Properties(options, app));

因此,服务properties实际上在创建时就收到了app对象。

了解此内容使我看到了正确的解决方案,像这样写在src/services/properties/properties.class.js

const { Service } = require('feathers-sequelize');

exports.Properties = class Properties extends Service {

    constructor(options, app) {
        super(options, app);
        this.app = app;
    }

    async find(data, params) {
        let oldRes = await super.find(data, params);
        let newResData = oldRes.data.map(async pr => {
            let includedRecords = await this.app.service('propertyadds').find({
                query: {
                    property_id: pr.id
                }
            })
            pr.additionals = includedRecords.map(e => e.additional_id).sort();
            return pr;
        })
        return await Promise.all(newResData)
            .then(completed => {
                return {
                    total: oldRes.total,
                    limit: oldRes.limit,
                    skip: oldRes.skip,
                    data: completed
                }
            })
    }
}

正如可以看到的,这是为扩展的Properties类创建constructor方法的问题,以便在app中公开this.app对象。可以在调用super(options,app)使this可用之后完成。

此后,仅需使用this.app创建其他服务的实例,然后进行正确的异步调用。


0
投票

事实上,当我访问src/services/properties.service.js并找到此行时,解决方案就实现了>

app.use('/properties', new Properties(options, app));
© www.soinside.com 2019 - 2024. All rights reserved.