I am new to express js and sequelize. I want to display all users and their orders(can be more than 1).
My code is
app.get('/test', function (req, res) {
var arr = [];
db.user.findAll().then(
function (resp) {
for (i = 0; i < resp.length; i++) {
arr.push(Object.assign({},
{
user_id: resp[i]['id'],
user_email: resp[i]['email'],
data: x(resp[i]['id'])
}))
}
res.send(arr);
}
);
});
and the function that will give me orders of particular user
var x = function (n) {
var arr1 = [];
db.order.findAll({ where: { userId: n } }).then(function (response) {
for (i = 0; i < response.length; i++) {
arr1.push(Object.assign({},
{
order_id: response[i]['id'],
order_total: response[i]['totalAmount']
}));
}
});
return arr1;
}
This is only showing me all users but not their particular orders, the array length returned by function 'x' is 0, I know this problem is related to promises and resolved results. I have tried .resolve() , .success() but nothing works . How can I fix it ? Thanks