Update:
I have a async function as follows:
async function userHandler(username, displayName, profilePicture, email) {
connection = await connectDB()
await connection.query('USE spyncdb;');
await connection.query('SELECT * FROM users WHERE username = ?', [username], function(error, result, fields) {
if (error) {
console.log(error);
}
if (result) {
if (result != 0) {
console.log('User already in database')
return result[0].user_id;
// Do whatever should be done
} else {
// Add user to database
connection.query('INSERT INTO users (username, displayname, profilePicture, email) VALUES (?, ?, ?, ?)', [username, displayName, profilePicture, email], function(error, result, fields) {
if (error) {
console.log('ERROR');
console.log(error);
}
if (result) {
console.log('user inserted into db');
return;
};
})
}
}
});
}
I then call this function, and want to store the return value from it (user_id).
I call the function from the following:
async () => {
let user_id = await userHandler(aUser.username, aUser.displayName,
aUser.profilePicture, aUser.email);
console.log(user_id);
}
But I only get "undefined" - why?
PS. I use the mysql library for my DB connection.