0

so I'm trying to chaing some SELECT sql queries with tedious promises and can't seem to be able to do so. I get the results I want through the query but it keeps returning undefined.

Here's some of the code:

function getSoma(tableName, cod, index) {
  let somaResult;
  tp.sql(
    `DECLARE @tableName varchar(1000)
  SET @tableName = 'SELECT soma from tbs_cod_${tableName} WHERE cod=${cod}'
  EXEC (@tableName)`
  )
    .execute()
    .then(async (results) => {
      // do something with the results
      somaResult = results;
    })
    .fail(function (err) {
      // do something with the failure
      console.log("nay" + err);
    });
  return somaResult;
}

and here's where I call the getSoma function in order to push it to another array, but it keeps showing undefined

let codesSum;
  // post para a API para obter o array de código
  axios
    .post("http://138.68.29.250:8082", qs.stringify(req.body), {
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
    })
    .then((response) => {
      codesSum = response.data.split(/[A-Za-z]*#/gi).filter(Boolean);
      codesArray.map((element, index) => {
        return (element.cod = Number(codesSum[index]));
      });
      executeStatementMultipleInsert(codesArray);
      console.log(codesArray);
    })
    .then(() => {
      codesArray.forEach(async (element, index) => {
        console.log(getSoma(Object.keys(element)[0], element.cod, index))
      });
    })
    .then(() => console.log(codesSum))
    .catch((err) => console.log(err));

Any clues on how to solve this? I suspected it might have something to do with asynchronicity but I'm kinda stuck.

EDIT 1: by using the await I could get the results from the getSoma function but I can't seem to push the results to another array like in this code

 let codesSum;
  let soma;
  // post para a API para obter o array de código
  axios
    .post("http://138.68.29.250:8082", qs.stringify(req.body), {
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
    })
    .then((response) => {
      codesSum = response.data.split(/[A-Za-z]*#/gi).filter(Boolean);
      codesArray.map((element, index) => {
        return (element.cod = Number(codesSum[index]));
      });
      executeStatementMultipleInsert(codesArray);
      console.log(codesArray);

      codesArray.forEach(async (element) => {
        soma = await getSoma(Object.keys(element)[0], element.cod);
        console.log(soma);
        codesSum.push(soma);
      });
      console.log(codesSum)
    })
    .catch((err) => console.log(err));
    ```
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Yousaf May 18 '21 at 07:45
  • 1. Inside the `getSoma` function, get rid of `somaResult` variable and simply return the result of calling `tsql(...).execute.then(...)`. 2. Inside the callback of the `then()` method, just return the `result` instead of assigning it to `someResult`. 3. Remove the `async` keyword from the callback function of the `then()` method. 4. Remove `fail` method call or throw the error instead of just logging it. – Yousaf May 18 '21 at 07:47
  • this didn't work, cause when i return the result of calling the tsql it just returns me a promise – Matheus Ciappina Pereira May 18 '21 at 08:48
  • _"it just returns me a promise"_ - either call `then()` method on that promise or `await` it. Also make sure you return the `result` from the callback function of the `then()` method – Yousaf May 18 '21 at 08:54
  • yeah, actually i tried that again by doing the await and it worked, but I still can't push these results to an array after I call the get Soma functions, like in the edit – Matheus Ciappina Pereira May 18 '21 at 09:06
  • use `for of` loop instead of `forEach` method to call the `getSoma` function multiple times – Yousaf May 18 '21 at 09:09

0 Answers0