0

This is my code

async function checksubsdetails() {
  try {
    let update_data = [];
    db_office.query(
      `SELECT arn_no,arn_id,cams_email from no_of_arn WHERE cams_email !=''`,
      async (err, arn_rows) => {
        if (err) throw err;

        for (let inx of arn_rows) {
          // console.log(inx.arn_no)
          await db_office.query(
            `SELECT arn_no from cams_subs WHERE arn_no='${inx.arn_no}'`,
            async (err, subs_rows) => {
              if (err) throw err;
              // console.log(subs_rows)
              if (subs_rows.length == 0) {
                await db_office.query(
                  `INSERT INTO cams_subs(arn_id, arn_no) values('${inx.arn_id}', '${inx.arn_no}')`,
                  (err, my_cursor) => {
                    if (err) throw err;
                    console.log(("New Data Added for Arn no -> ", inx.arn_no));
                    update_data.push(inx);
                  }
                );
              } else {
                console.log("Data Already Exists for arn no ->", inx.arn_no);
                //console.log(("New Data Added for.lo"))
                update_data.push(inx);
              }
            }
          );
        }
        return [2, update_data];
      }
    );
  } catch (err) {
    console.log(err);
    return [1, "Exception While adding Data to Cams Subs"];
  }
  
}

I am returning [2, update_data] but getting undefined for update_data.

I am not able to figure out what to do to resolve this because the further processes can only occur after getting this value.

update_data is giving result in if-else statement I have written but I want it to return the update_data array after for loop.

Please help me out in this.

Thanks

Vishal
  • 99
  • 1
  • 5
  • 3
    You're returning from an asynchronous callback, not returning from your function itself. That won't do you any good as the return value doesn't go anywhere. You either have stop using `async` and communicate the return results back with a plain callback or convert all your asynchronous functions to use promises instead of plain callbacks and then, and only then, can you use an async function properly. `async` functions only work with promise-based asynchronous operations, not with plain callback asynchronous functions. – jfriend00 Mar 10 '22 at 06:32
  • As an aside: this code is vulnerable to SQL injection attacks, and unless run in a properly exclusive transaction, race conditions. – AKX Mar 10 '22 at 06:36

2 Answers2

0

@jfriend00 explained the problem – here's one possible solution that uses Node.js's util.promisify function to take the db_office.query function and turn it into something that can actually be awaited upon.

Please heed the comments in the code.

async function checksubsdetails() {
  // Promisify the `db_office.query` function (depending on the library,
  // it may already have a promisified counterpart; please read the docs).
  const queryP = util.promisify(db_office.query.bind(db_office));
  const update_data = [];

  const arn_rows = await queryP(
    `SELECT arn_no, arn_id
     from no_of_arn
     WHERE cams_email !=''`,
  );
  for (let inx of arn_rows) {
    // TODO: this is susceptible to race condition attacks since multiple processes
    //       may be doing this loop simultaneously
    const subs_rows = await queryP(
      `SELECT arn_no
       from cams_subs
       WHERE arn_no = '${inx.arn_no}'`, // TODO: fix SQL injection vulnerability
    );
    if (subs_rows.length === 0) {
      await queryP(
        `INSERT INTO cams_subs(arn_id, arn_no)
         values ('${inx.arn_id}', '${inx.arn_no}')`, // TODO: fix SQL injection vulnerability
      );
      console.log("New Data Added for Arn no -> ", inx.arn_no);
    } else {
      console.log("Data Already Exists for arn no ->", inx.arn_no);
    }
    update_data.push(inx);
  }
  return update_data;
}
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thanks brother. This code is working and I am getting the data in return statement now. I am an intern and just started to work on real projects. I have one question, my code was hitting the sql queries before making the connection? or why it update_data was giving undefined because I have initialized it in the async function? – Vishal Mar 10 '22 at 07:20
  • Because you were using APIs that aren't await-safe at all; you weren't awaiting for anything. – AKX Mar 10 '22 at 07:22
0

Inside function line no 4, you are calling db_office.query() without await. So this promise was not fulfilled. So, just after running the function, that promise is getting added in the asynchronous event loop and returned. You are not waiting to get the promise fulfilled. When that promise is fulfilled after the return, you are seeing the output in the console. Try with await and return your return after the promise is fulfilled

await db_office.query()
// return after every promise is fulfilled