As I navigate my way through an express route, I am puzzled as to why the "purchasedCards" array turns out empty after going through these database calls. Despite successfully gathering all the necessary information from various DB Queries and placing it in the context for frontend transmission, the object just won't get pushed into the array...
This is my first time exploring the postgres database / SQL realm. Any pointers or suggestions would definitely be welcomed.
//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)
})
})
})