'Express: MySQL query doesn't push code to a list
I have been trying to push the results of MySQL query to a list(shoppingList), but the shoppingList is always empty, even though I'm getting 2 results back.
I think the problem is somewhere with handling the promise, but I haven't been able to figure it out:
static async showShoppingList(userId) {
let shoppingList = [];
const sql =
"SELECT item_name, family_member_name FROM shopping_list WHERE user_id = (?)";
await db.promise().query(sql, [userId], function (err, results) {
for (const result of results) {
shoppingList.push(result)
console.log(shoppingList) // Here it appears with results
}
});
console.log(shoppingList) // Here it appears empty
return shoppingList;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
