Javascript 如何在FeatherJS服务扩展中使用app.service(';myService';)?
我正在尝试为我的FeatherJS应用程序中名为Javascript 如何在FeatherJS服务扩展中使用app.service(';myService';)?,javascript,feathersjs,Javascript,Feathersjs,我正在尝试为我的FeatherJS应用程序中名为properties的服务扩展find()方法 我需要的是在find()返回的所有记录中添加一个数组,其中包含来自另一个名为propertyadds的服务的整数。这意味着我需要从 { "total": 2973, "limit": 10, "skip": 0, "data": [ { "id": 1, ... }, { "id": 2, ... }
properties
的服务扩展find()
方法
我需要的是在find()
返回的所有记录中添加一个数组,其中包含来自另一个名为propertyadds
的服务的整数。这意味着我需要从
{
"total": 2973,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
...
},
{
"id": 2,
...
}
...
]
}
到
其中,附加值来自servicepropertyadds
,它管理一个类似
| 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的新手)我不知道如何获得它
我需要什么才能让一个有效的
应用程序常量访问此模块内的所有服务?我不知道feather sequelize的具体情况,但根据
对于在调用app.listen之前注册的服务,在调用app.listen时调用每个注册服务的设置函数
通过继承服务
(我不知道这是否是标准的方法),可能服务
实现了设置
,您可以从属性::查找()中访问这个.app
如果没有,请写下:
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
}
我不知道羽毛续集,但根据
对于在调用app.listen之前注册的服务,在调用app.listen时调用每个注册服务的设置函数
通过继承服务
(我不知道这是否是标准的方法),可能服务
实现了设置
,您可以从属性::查找()中访问这个.app
如果没有,请写下:
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
}
})
}
}
可以看出,这是一个为扩展属性类创建构造函数的问题,以便在this.app
中公开app
对象。这可以在调用super(选项,应用程序)
使此功能可用后完成
在此之后,只需使用this.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
}
})
}
}
可以看出,这是一个为扩展属性类创建构造函数的问题,以便在this.app
中公开app
对象。这可以在调用super(选项,应用程序)
使此功能可用后完成
在此之后,只需使用this.app
创建另一个服务的实例,然后进行正确的异步调用即可。谢谢您的编辑@ishenwood!谢谢你的编辑,@ishenwood!也许我做错了,但当我尝试这一切时,我得到的只是一个错误super.setup(app,path)TypeError:(中间值)。setup不是一个函数
…很抱歉,sequelize::Service没有定义设置,因此无需调用父方法。那么,如何获取有效的app对象?或者我不能这么做?你可以试试上面的版本,或者只是像文档中那样setup(app){this.app=app}
。如果有一天您更新了feathers sequelize,并且由于某些不清楚的原因,它现在实现了一个设置,那么最好将其命名为。。。但现在你是“安全的”也许我做错了,但当我尝试这一切时,我得到的只是一个错误super.setup(app,path)TypeError:(中间值)。setup不是一个函数
…很抱歉,sequelize::Service没有定义设置,因此无需调用父方法。那么,如何获取有效的app对象?或者我不能这么做?你可以试试上面的版本,或者只是像文档中那样setup(app){this.app=app}
。如果有一天您更新了feathers sequelize,并且由于某些不清楚的原因,它现在实现了一个设置,那么最好将其命名为。。。但现在无论哪种方式,你都是“安全的”