Typescript - Why oracledb execute() function return type is 'unknown'?

35 Views Asked by At

I am facing below error when I am trying to access the cursor 'p_activerecords' returned by the stored procedure.

'dbresult.outBinds' is of type 'unknown'.ts(18046)

Reason is, 'await dbconn.execute(sql, bindVar, options);' return type is unknown.
Now, this error can be resolved by updating type of dbresult to any as below.

const dbresult: any = await dbconn.execute(sql, bindVar, options);

However, I am not comfortable with this workaround and would like to know if there is any way to make sure that execute() returns 'OracleDB.Result' instead of 'unknown'.

import { dashboardRecord, dashboardRecordsRequest, dashboardRecordsResponse } from "datamodel/dashboard/dashboard";
import { logger } from "logger/logger";
import OracleDB from "oracledb";

export async function getActiveRequestRecords(dashboardRecordsRequest: dashboardRecordsRequest): Promise<dashboardRecordsResponse> {
    let dashboardRecordsResponse: dashboardRecordsResponse;
    dashboardRecordsResponse = {
        message: "",
        records: [],
        request: dashboardRecordsRequest,
        totalRecords: 0,
    }
    const sql: string = `BEGIN get_dashboard_active_request_records(:p_entityname, :p_sortby, :p_sortorder, :p_startindex, :p_pagesize, :p_totalrecords, :p_activerecords); END;`;
    const p_entityname: OracleDB.BindParameter = { dir: OracleDB.BIND_IN, val: dashboardRecordsRequest.entityName, type: OracleDB.STRING };
    const p_sortby: OracleDB.BindParameter = { dir: OracleDB.BIND_IN, val: dashboardRecordsRequest.sortBy, type: OracleDB.STRING };
    const p_sortOrder: OracleDB.BindParameter = { dir: OracleDB.BIND_IN, val: dashboardRecordsRequest.sortOrder, type: OracleDB.STRING };
    const p_startindex: OracleDB.BindParameter = { dir: OracleDB.BIND_IN, val: dashboardRecordsRequest.startIndex, type: OracleDB.NUMBER };
    const p_pagesize: OracleDB.BindParameter = { dir: OracleDB.BIND_IN, val: dashboardRecordsRequest.pageSize, type: OracleDB.NUMBER };
    const p_totalrecords: OracleDB.BindParameter = { dir: OracleDB.BIND_OUT, type: OracleDB.NUMBER };
    const p_activerecords: OracleDB.BindParameter = { dir: OracleDB.BIND_OUT, type: OracleDB.CURSOR };
    const bindVar: OracleDB.BindParameter[] = [
        p_entityname,
        p_sortby,
        p_sortOrder,
        p_startindex,
        p_pagesize,
        p_totalrecords,
        p_activerecords
    ];
    const options: OracleDB.ExecuteOptions = { outFormat: OracleDB.OUT_FORMAT_OBJECT };

    try {
        const dbconn = await OracleDB.getConnection('requestpool');
        const dbresult = await dbconn.execute(sql, bindVar, options);
        const activerecords = dbresult.outBinds.p_activerecords;
        const rows = await activerecords?.getRows();
        dashboardRecordsResponse.totalRecords = dbresult.outBinds.p_totalrecords;

        let dashboardRecord: dashboardRecord;
        for (const row of rows) {
            dashboardRecord = {
                approvedDate: row.ACTIONDATE,
                creationDate: new Date(Date.now()),
                employeeGEN: '',
                employeeName: '',
                lastActionDate: row.ACTIONDATE,
                pendingWith: row.PENDINGWITH,
                processType: row.PROCESSTYPE,
                requestNumber: row.REQUESTNUMBER,
                requestorName: row.REQUESTOR,
                requestState: row.REQUESTSTATUS,
                submissionDate: new Date(Date.now()),
                withdrawnDate: new Date(Date.now()),
            }
            dashboardRecordsResponse.records?.push(dashboardRecord);
        }
        await dbconn.close();
    } catch (err) {
        dashboardRecordsResponse.message = 'Error while retrieving Active Request records.';
        logger.error(err);
    }
    console.log(dashboardRecordsResponse);
    return dashboardRecordsResponse;
}
0

There are 0 best solutions below