简体   繁体   中英

How to push to array of object based on condition?

I am trying to push responses to array based on a conditional statement. The response is in request details object it would be nested object with different LOBS so it could be both or one lob object in the request. If both lobs are present it works fine. However, when I pass just one combineResponse method the code is failing because one of the arguments is not passed. How can I resolve this issue or better yet, is there a better approach to handle this?

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;
    }

You need a default value or a check for null or undefined. Try:

 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;
    }

What I did:

  1. Removed default assignments for _dataAdmin and _dataUser.
  2. Fixed the push to only push values for arguments that are defined.
  3. Changed if logic on "partial Error" code block to check if variables are defined first and to return partial error if just one of the statusCodes is wrong(what I think was the intent of the code)

What I assumed: The await calls return null or undefined if the request fails entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM