如何在 Loopback4/Typescript 中对 API 密钥进行额外查找后,根据父 ID 强制过滤数据



我是打字稿和循环(lb4)的菜鸟,我正处于一个症结所在,我有点拉扯我的头发,我希望社区能够提供帮助。我正在尝试根据针对数据库记录的 API 键完成对记录 ID 的查找,因此我可以将此记录用作外键,以便从另一个数据库表派生另一组结果。我正在使用预先存在的数据库

错误显示为如下。

环回:连接器:mssql 结果:空 {"记录集":[[]],"记录集":[],"输出":{},"行受影响":[0]} +302ms/users/bloop/Desktop/AngularTestFolder/ZaharaReporting/zaharareporting/dist/controllers/tenancy.controller.js:27 返回对象.tenancyid.valueOf; ^

类型错误: 无法读取 null 的属性"租户 id">

我已经尝试了很多事情,并怀疑这是缺乏对打字稿或loopback4的一些基础知识的理解,所以如果是这种情况,我很抱歉,我只是发现自己在如何绕过它方面有同样的想法,所以我觉得需要有人指出我为什么以及如何愚蠢请

。我正在尝试让端点/api/{apikey}/GetSpecificBusinessunit'工作。 但我收到"FindTenancyID.IdOnly"返回未定义

import {
repository,
} from '@loopback/repository';
import {
param,
get,
} from '@loopback/rest';
import { Tenancy, Businessunit } from '../models';
import { TenancyRepository, BusinessunitRepository } from '../repositories';
class TenancyHelper {
constructor(@repository(TenancyRepository)
public tenancyRepository: TenancyRepository) { }
// Query filter tenancy to get Id only
async idOnly(api: String) {
return await this.tenancyRepository.findOne(
{ where: { apikey: api }, fields: { tenancyid: true }, limit: 1 },
function (err: any, object: Tenancy) {
//console.log("TenancyId was " + object.tenancyid);
return object.tenancyid.valueOf;
}).catch(a => console.log(a));
}
}
export class TenancyController {
constructor(
@repository(TenancyRepository)
public tenancyRepository: TenancyRepository,
@repository(BusinessunitRepository)
public businessunitRepository: BusinessunitRepository,
) { }

@get('/api/{apikey}/GetSpecificBusinessunit', {
responses: {
'200': {
description: 'Get BusinessUnit by API Key',
content: { 'application/json': { schema: { 'x-ts-type': Businessunit } } },
},
},
})
async GetBusinessunit(
@param.path.string('apikey') Apikey: String,
FindTenancyId = new TenancyHelper(this.tenancyRepository),
): Promise<Businessunit[]> {
return new Promise<Number>(function (resolve, reject) {
Number(FindTenancyId.idOnly(Apikey));
})
.then(a => {
console.log("This was the excecuted: " + a);
return this.businessunitRepository.find(
{ where: { tenancyid: a } })
})
}

我希望返回一个 JSON 对象,其中包含返回路径中定义的此租户 API 密钥的所有业务单位。

我没有正确使用承诺(可能还没有),但这对我来说是正确的

在 LoopBack 4 中,我们避免使用.then().catch()API,转而使用await和常规try/catch块。

export class TenancyController {
//...
async GetBusinessunit(
@param.path.string('apikey') Apikey: String
): Promise<Businessunit[]> {
const a = await this.tenancyRepository.findOne(
{ where: { apikey: Apikey }, fields: { tenancyid: true }, limit: 1 }
);
let id = -1;
if (a !== null) {
id = Number(a!.tenancyid);
}
return this.businessunitRepository.find(
{ where: { tenancyid: id } })
});
}
}

只是对我自己问题的回答,以防有人想要尝试类似的结果。

我没有正确使用承诺(可能还没有),但这对我来说是正确的。

@get('/api/{apikey}/GetBusinessunits', {
responses: {
'200': {
description: 'Get BusinessUnits for API Key',
content: { 'application/json': { schema: { 'x-ts-type': Businessunit } } },
},
},
})
async GetBusinessunit(
@param.path.string('apikey') Apikey: String
): Promise<Businessunit[]> {
return await this.tenancyRepository.findOne(
{ where: { apikey: Apikey }, fields: { tenancyid: true }, limit: 1 }
).then((a) => {
let id = -1;
if (a !== null) {
id = Number(a!.tenancyid);
}
return this.businessunitRepository.find(
{ where: { tenancyid: id } })
})
}

最新更新