Answer the question
In order to leave comments, you need to log in
How do I make an array of received values from mysql?
Made a request to the database
let user = ctx.state.user[0];
let sql = 'SELECT id_theme FROM access WHERE id_user = ?';
let themes = db.query(sql, [user.id], async (err, result) => {
if(err) console.error(err);
console.log(result);
await result;
});
[ RowDataPacket { id_theme: 1 },
RowDataPacket { id_theme: 2 },
RowDataPacket { id_theme: 3 },
RowDataPacket { id_theme: 2 },
RowDataPacket { id_theme: 3 } ]
each theme in themes
p=theme
Cannot convert object to primitive value
await JSON.stringify(result);
Answer the question
In order to leave comments, you need to log in
If you definitely want to write the words async and await, and you definitely want to get the result of the query into the themes variable, then you need to write something like this:
asyncFoo();
async function asyncFoo() {
var themes = await new Promise( resolve => {
var sql = 'SELECT id_theme FROM access WHERE id_user = ?';
db.query(sql, [user.id], (err, result) => {
if(err) console.error(err);
resolve(result);
});
}).then( result => {
return result;
};
console.log(themes);
// тут его в pug и передавать
}
Well, actually, an array is returned in the callback
And I don’t know how pug works with it.
There is a suspicion that this problem is due to incorrect work with async / await - why is it even there?
let themes = db.query(sql, [user.id], async (err, result) => {
if(err) console.error(err);
console.log(result);
await result;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question