0

I have written some code to call a procedure,it's OUT parameter type is cursor(ResultSet),so i have to fetch data from ResultSet,for that i've written one function(fetchRowsFromRS()) which extract data from ResultSet.

  1. I've used return statement in fetchRowsFromRS(),but not returning anything, getting undefined.
  2. When i called fetchRowsFromRS(),control is not pausing execution of next lines of code(i'have used Async/await) this is required because i want use extracted data in next lines.

What is the mistake in my code?

db.js

    connection.execute(plsql,bindvars,options,async function (err, result) {
          if (err) {
            console.log(err);
            console.error(err.message);
            doRelease(connection);
            return;
          }

      if(result.outBinds.OUT_STATUS=='SUCCESS'){

        if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

        loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

        loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
       }else{

   loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

   loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User 
       logged in successfully');
       var numRows=20;
       //calling function to fetch data from ResultSet
       var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
    console.log('----------'+rsData);//giving undefined

    //here i want to use ResultSet Data

        }
      }
  })

function to extract data from ResultSet(not returning anything)

function fetchRowsFromRS(connection, resultSet, numRows) {
     resultSet.getRows(numRows,function (err, rows) {
          if (err) {
            console.error(err);
            doClose(connection, resultSet);   // always close the ResultSet
          } else if (rows.length > 0) {
          console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
          console.log(rows); //getting data here


          if (rows.length === numRows)      // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows);
          else
            doClose(connection, resultSet); // always close the ResultSet


        } else { // no rows
          doClose(connection, resultSet);   // always close the ResultSet
        }
        return rows;
      });
  }
viswanath
  • 13
  • 5
  • *"I've used return statement in `fetchRowsFromRS`"*: no, you have not. You have a `return` in a callback function, which is a different function. – trincot Nov 18 '18 at 14:08
  • What is `connection.execute`? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an `async function` as a callback. – Bergi Nov 18 '18 at 14:30
  • 3
    Possible duplicate of [How to use async await function object in Javascript?](https://stackoverflow.com/questions/34210812/how-to-use-async-await-function-object-in-javascript) – NAVIN Nov 18 '18 at 14:56
  • There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem. – NAVIN Nov 18 '18 at 14:57
  • @NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js – viswanath Nov 18 '18 at 17:00

2 Answers2

0

fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.

Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
0
function fetchRowsFromRS(connection, resultSet, numRows) {
  return resultSet
    .getRows(numRows)
    .then(function(rows) {
      if (rows.length > 0) {
        console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
        console.log(rows) //getting data here
        if (rows.length === numRows)
          // might be more rows
          fetchRowsFromRS(connection, resultSet, numRows)
        else doClose(connection, resultSet) // always close the ResultSet
      } else {
        // no rows
        doClose(connection, resultSet) // always close the ResultSet
      }
      return rows
    })
    .catch(function(err) {
      if (err) {
        console.error(err)
        doClose(connection, resultSet) // always close the ResultSet
      }
    })
}

The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.

await fetchRowsFromRS(connection, resultSet, numRows)

will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.

OR

Use new Promise

function fetchRowsFromRS(connection, resultSet, numRows) {
  return new Promise(function(resolve, reject) {
    resultSet.getRows(numRows, function(err, rows) {
      if (err) {
        console.error(err)
        doClose(connection, resultSet) // always close the ResultSet
        reject(err)
      } else if (rows.length > 0) {
        console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
        console.log(rows) //getting data here

        if (rows.length === numRows)
          // might be more rows
          fetchRowsFromRS(connection, resultSet, numRows)
        else doClose(connection, resultSet) // always close the ResultSet
      } else {
        // no rows
        doClose(connection, resultSet) // always close the ResultSet
      }
      resolve(rows)
    })
  })
}
Jeevan
  • 309
  • 1
  • 6
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! And in your first snippet, make sure to `return` the inner promises from the `then` callback or they won't be awaited. – Bergi Nov 18 '18 at 15:23
  • @Jeevan Thanks a lot! for you suggestion.I've used **resultSet.getRows(numRows)** it returns a promise. – viswanath Nov 19 '18 at 10:24