When i call my own api developed using node.js, connection on postgres (Sequelize) database, it's return the follow JSON:
[ { "id": 1, "name": "Wallet Name", "wallet_type": "MN", "icon": "fa fa-bank", "color": "#000000", "credit_limit": 3000, "due_day": 22 } ] I just need, it return one more line (account_value) on each object, that's info is inside another javascript function, so it's should look as:
[ { "id": 1, "name": "Wallet Name", "wallet_type": "MN", "icon": "fa fa-bank", "color": "#000000", "credit_limit": 3000, "account_value": 1200.55, "due_day": 22 } ] My current code is:
async index(req, res) { const wallets = await Wallet.findAll({ where: {}, attributes: [ 'id', 'name', 'wallet_type', 'icon', 'color', 'credit_limit', 'due_day', ], order: [['name', 'ASC']], }); const finalObject = []; wallets.forEach(async wallet => { const currentItem = wallet.dataValues; const { id } = await currentItem; const { sum_credits } = await WalletsResume.sumCredits(id); const { sum_debits } = await WalletsResume.sumDebits(id); const sum_account_value = (sum_credits - sum_debits).toFixed(2); currentItem.account_value = sum_account_value; finalObject.push(currentItem); console.log(`pushed ${id}`); }); console.log(`-------------------------`); console.log(finalObject); return res.json(finalObject); } But, when its return a empty array:
[] Do you can help me please?
I have no idea how to fix it (i can change all my code)
Thank you so much!