I hate to ask such a simple question but I can't find an answer anywhere.
I am trying to push the sql results to results2, if I print out the results2 value inside of the connection query it shows the array but when I try to return or print it outside it shows as undefined.
Code:
function getUserData(cookie){
const sqlquery = "SELECT * FROM `users` WHERE `cookie` = ?"
const params = [cookie];
let results2 = [];
connection.query(sqlquery, params, function(error, results, fields){
if(error) throw error;
if(results && results.length){
results2.push(results[0]);
}else{
return("invalid");
}
})
console.log(results2)
return(results2);
}
I have searched and read through documentations but I can't find where I am messing up.