mysql async/await in Node.js just returns undefined
P粉821274260
P粉821274260 2024-03-27 12:32:47
0
2
371

I've been trying to solve this problem for two days now and have looked through basically everything I can find on stackoverflow, maybe I'm missing something since I've been staring at it for so long.

I have a nodejs file that loads user data via sql and sends the results to the main file (server).

I call this function again using an async function to get the value of the row.


var sql = require('./db');
let h = /^[a-zA-Z-_.\d ]{2,180}$/;

 async function getdata(hash){
if (!hash || !hash.match(h)) {
    return {type: "error", msg:"Verarbeitungsfehler, bitte laden Sie die Seite neu.", admin:     false}
}else{

    const response = await sql.query('SELECT * FROM bnutzer WHERE hash = ?',[hash], async     function(err, result, fields){
        //console.log(result)
        return await result
    })

    }   
}




module.exports = { getdata };

async function getvalue(h){

    try{

     var result = await admin.getdata(h); 
    console.log('1'+result);
     if(result{

        console.log('2'+result)
     }

    }catch(err){
        console.log(err);
        console.log('Could not process request due to an error');
        return;

    }
}

getvalue(user.ident)

The data from the sql query is correct and when I output them in the console they also return the correct results, however they are not passed back to the function that I call the code, it's just undefined and I have The feeling of waiting here is somehow not waiting for results, what am I missing here? I've tried it with a few zodiac signs I could find online, and unfortunately I haven't gotten there yet.

I have tried writing the sql query without using async/await and just writing the calling function using async/await, but with no results. I have tried various sql queries with and without sql callbacks. I'm hoping someone can point out something I'm twisting or missing here.

P粉821274260
P粉821274260

reply all(2)
P粉194919082

You are executing the query incorrectly, you should do this

async function getdata(hash){
    if (!hash || !hash.match(h)) {
        return {type: "error", msg:"Verarbeitungsfehler, bitte laden Sie die Seite neu.", admin:     false}
    }else{
        
      try{
        const response = await sql.query('SELECT * FROM bnutzer WHERE hash = ?',[hash])
        return response
      }
      catch(error){
        throw Error(error.message + "error")
      }
     } 
 }

Try it, you can't mix callbacks and awaits like you did, just wait for the result, if an error occurs, in that case it will be handled by the catch block

P粉386318086

In the good case, your async callback function will just return it into the response variable. But anyway, using return wait in nested async functions is not the best approach. So your result will become the response variable, you are not doing anything with it, it will just stay as is, with no return value. So you get a normal function and due to its execution the result is undefined . Because you're not returning anything from there.

I recommend:

const sql = require('./db');
const h = /^[a-zA-Z-_.\d ]{2,180}$/;

async function getdata(hash){
  if (!hash || !hash.match(h)) {
    return {type: "error", msg:"Verarbeitungsfehler, bitte laden Sie die Seite neu.", admin:     false}
  }else{
      try {
      const response = await sql.query('SELECT * FROM bnutzer WHERE hash = ?',[hash]);
      return response;
    } catch (err) {
      throw new Error('Cold not proceed with query');  // Wwhatever message you want here
    }
  }   
}


module.exports = { getdata };
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!