我试图基于条件语句将响应推送到数组。响应位于请求details
对象中,该对象将是具有不同LOBS
的嵌套对象,因此它可以是请求中的两个或一个lob对象。如果两个瓣都存在,则工作正常。但是,当我仅传递一种combineResponse
方法时,代码失败,因为未传递arguments
之一。我该如何解决这个问题,或者更好呢?是否有更好的方法来解决这个问题?
main.ts
private async execute(@Request() request: ExpressRequest): Promise<any> {
let _combineData: any = [];
const accountBalanceRequest = request.body.getAccountBalanceRequest;
const LOB = accountBalanceRequest.details.lineofBusiness;
let _dataAdmin: any = {};
let _dataUser: any = {};
try {
if (LOB === "ADMIN") {
_dataAdmin = await this.Admin.adminRequestHandler(accountBalanceRequest);
}
if (LOB === "USER") {
_dataUser = await this.User.userRequestHanlder(request);
}
const combinedResponses = this.combineResponse(_dataAdmin, _dataUser, request);
_combineData = combinedResponses;
} catch (err) {
return err;
}
return _combineData;
}
private combineResponse(AdminResponse: any, UserResponse: any, request: any): any {
const combinedResponses: any = [];
let finalResponse: any = {};
const refID: string = request.body.header.serviceContext.refID;
const tokenID: string = request.body.header.serviceContext.tokenID;
combinedResponses.push(AdminResponse, UserResponse);
if (AdminResponse.header.statusCode === "0000" && UserResponse.header.statusCode === "0000") {
finalResponse = {
getAccountBalanceResponse: {
header: request.body.header),
details: combinedResponses
}
};
}
if (AdminResponse.header.statusCode !== "0000" && UserResponse.header.statusCode !== "0000") {
finalResponse = {
getAccountBalanceResponse: {
header: {
statusCode: "9999",
statusDesc: "partial Error",
refID,
tokenID
},
details: combinedResponses
}
};
}
return finalResponse;
}
答案 0 :(得分:0)
您需要一个默认值,或者检查是否为null或undefined。 试试:
private async execute(@Request() request: ExpressRequest): Promise<any> {
let _combineData: any = [];
const accountBalanceRequest = request.body.getAccountBalanceRequest;
const LOB = accountBalanceRequest.details.lineofBusiness;
let _dataAdmin: any;
let _dataUser: any;
try {
if (LOB === "ADMIN") {
_dataAdmin = await this.Admin.adminRequestHandler(accountBalanceRequest);
}
if (LOB === "USER") {
_dataUser = await this.User.userRequestHanlder(request);
}
const combinedResponses = this.combineResponse(_dataAdmin, _dataUser, request);
_combineData = combinedResponses;
} catch (err) {
return err;
}
return _combineData;
}
private combineResponse(AdminResponse: any, UserResponse: any, request: any): any {
const combinedResponses: any = [];
let finalResponse: any = {};
const refID: string = request.body.header.serviceContext.refID;
const tokenID: string = request.body.header.serviceContext.tokenID;
if (AdminResponse) {
combinedResponses.push(AdminResponse);
}
if (UserResponse) {
combinedResponses.push(UserResponse);
}
if (AdminResponse.header.statusCode === "0000" && UserResponse.header.statusCode === "0000") {
finalResponse = {
getAccountBalanceResponse: {
header: request.body.header),
details: combinedResponses
}
};
}
if ((!AdminResponse || AdminResponse.header.statusCode !== "0000") || (!UserResponse || UserResponse.header.statusCode !== "0000")) {
finalResponse = {
getAccountBalanceResponse: {
header: {
statusCode: "9999",
statusDesc: "partial Error",
refID,
tokenID
},
details: combinedResponses
}
};
}
return finalResponse;
}
我做了什么:
我假设: 如果请求完全失败,则await调用返回null或未定义。