0

I need to catch some data by a mysql query, and use the result to build up and email message with its results with node. I put the code inside a function, but the call to the query appear to still be async, as the result is never given back before the end of the function, and the returning variable is alwasy empty.

Tried different approach with async/await but still the execution seems async In my following code is just get in the console log up to the step 3, the step 4 is mde no matter what I try to do at the end of the function call

async function querydb (utente){
  console.log("sono in querydb");
  var messageHTMLAllegati="";
  var risultatoquery;
  console.log("step 1");
         var connection = mysql.createConnection({
    host     : process.env.IP_address,
    user     : process.env.nome_utente,
    password : process.env.password,
    port     : process.env.port,
    database : process.env.DB,
  });
  console.log("step 2");
  const query = util.promisify(connection.query).bind(connection);

(async () => {
  try {
    console.log("step 3");
    var result = await query('SELECT Link FROM Link_Foto where ID_Utente="' + utente + '"');
    var i = result.length;
    console.log("step 4");
  var j ;
  for (j=0; j < i; j++) {
        messageHTMLAllegati +='<a href="' + result[j].Link + '">Immagine ' + (j+1)+ '</a><BR>';
console.log("print the link found in the DB and added to the text to be printed"+result[j].Link);
        }
  } finally {
    connection.end();
  }
})()
  return messageHTMLAllegati;
}

I do expect the final variable "messageHTMLAllegati" to contain some text plus the query fields needed, but it get always empty. In the log I see though that the variable is filled up, but only after that the function is returned, therefore the text used to put the email together is empty from the DB section

1
  • Did you check console.log(query('SELECT Link FROM Link_Foto where ID_Utente="' + utente + '"');)? Is it returning promise? Commented Jun 11, 2019 at 10:28

1 Answer 1

1

async/await method only works when await functions is a promise. functions like 'query' in mysql are using a callback function to get the result. So if you want to use it with async/await method you should use it in another function and get the result in its callback function as a promise like this:

function query_promise(q_string){
    return new Promise((resolve, reject)=>{
        query(q_string,(err, result)=>{
        if(err) return reject(err);
        resolve(result);
    });
 });
}

then in your code:

var result = await query_promise('SELECT Link FROM Link_Foto where ID_Utente="' + utente + '"');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.