I am working on a route in express and I am not sure why my "purchasedCards" array is empty at the end of these database calls. I first collect all the information I need from a few DB Queries then I put it into the context to send to my front end. All the DB calls are working fine... Even the cardUpdator object. The only problem I have is getting the object pushed to the array...
I am new to using the postgres database /SQL. Any advice is certainly appreciated.
//User Account
router.get('/account', isLoggedIn, (req, res) => {
//Get User Info - First get the user info
db.query(`SELECT * FROM users WHERE userid = '${res.locals.user.id}';`)
.then(user => {
//Get user Purchases
db.query(`SELECT * FROM purchases WHERE newowner = '${res.locals.user.id}';`)
.then(purchases => {
let purchasedCards = []
//Get Card Info for Each Purchase
purchases.forEach( purchasedCard => {
//Get the card from user_cards table for standard card info.
db.query(`SELECT * FROM user_cards WHERE id = '${purchasedCard.card}';`)
.then( card => {
//Get Old Owner Info
db.query(`SELECT * FROM users WHERE userid = '${purchasedCard.oldowner}';`)
.then(oldOwner => {
let cardUpdator = {
cardName: card[0].name,
cardGame: card[0].game,
cardOldOwner: oldOwner[0].screen_name,
cardPID: purchasedCard.purchaseid,
cardTotal: purchasedCard.total,
cardId: purchasedCard.card,
}
purchasedCards.push(cardUpdator)
})
})
})
let context = {
name: user[0].screen_name,
email: user[0].email,
purchases: purchasedCards,
}
res.render('MyAccount.hbs', context)
})
})
})