I’m trying to get some information from a database in phpmyadmin using sequelize, but when I try to return the values I get "undefined". If I try to console.log the data, it appears just fine. I don’t really understand what the problem is.
UPDATE: I’ve tried using await getTweetInfo(), but I get an error saying that "await is only valid in async function". I need the string that this query should return for another function.
async function getTweetInfo(){
const data = await sequelize.query('SELECT contenido FROM tweets WHERE id = ?',
{replacements: [2], type:sequelize.QueryTypes.SELECT})
return data
}
getTweetInfo()
3
Answers
[UPDATE]
I thought it was obvious but let me give a few considerations.
example i gave is awaiting the result of the function and showing it in console.log()
getTweetInfo returns a Promise.
Try:
Or use await
The reason why you got the result is that you put the
console.log(data)
afterawait
. It will wait the query until it finishes and then next to yourconsole.log(data)
. That’s why you got the result.