sei-ec-remote / team-project-issues

0 stars 0 forks source link

Promise Chain not returning values that I'd like even when littering it with returns #111

Closed nabbott98 closed 1 year ago

nabbott98 commented 1 year ago

Describe the bug A clear and concise description of what the bug is. I saved a global empty array before my promise chain and then am trying to push true or false into the array based on if the item in the users cart is still in at least the

What is the problem you are trying to solve? I want to res. return an array detailing weather or not the quantity specified in the cart for each item is still available

Expected behavior A clear and concise description of what you expected to happen. return an array filled with booleans

like:

{
    "cartStatus": [true, false, true]
}

What is the actual behavior? A clear and concise description of what actually happened.

{
    "cartStatus": []
}

Post any code you think might be relevant (one fenced block per file)

router.get('/cart/status', requireToken, (req, res, next) => {
    cartStatus = []
    User.findOne({ _id: req.user.id })
        .then(handle404)
        .then((user) => {
            user.cart.forEach(element => {
                Item.findById(element.itemId)
                    .then(item => {
                        if(item.stock >= element.quantity){
                            cartStatus.push(true)
                            console.log('0: ', cartStatus)
                            return cartStatus
                        } else {
                            cartStatus.push(false)
                            console.log('0: ', cartStatus)
                            return cartStatus
                        }
                    })
                // console.log('1: ', cartStatus)
                // return cartStatus
            })
            // console.log('2: ', cartStatus)
            // return cartStatus
        })
        .then(() => res.status(200).json({ cartStatus: cartStatus }))
        // if `findById` is succesful, respond with 200 and "item" JSON
        .catch(next)
})

What is your best guess as to the source of the problem?

That I am missing returns or not passing down cartStatus through the promise chain

What things have you already tried to solve the problem? adding returns, console.logs, adding parameters and removing them

Additional context Add any other context about the problem here. Not that i can think of

Paste a link to your repository here routes branch https://github.com/nabbott98/global-back

kestler01 commented 1 year ago

Nesting promises like this is something we generally want to avoid - the dreaded 'promise hell' instead of querying in a loop at the 2nd nested promise, i think you should get all of the items in the db, and work with that array via a loop. It will be more straight forward and easier to read

kestler01 commented 1 year ago
    User.findOne({ _id: req.user.id })
        .then(handle404)
        .then((user) => { // < --- indentation! 😭
            user.cart.forEach(element => { // <-- instead get all items
                Item.findById(element.itemId)
                    .then(item => { // < -- will become items*
                       // now we have an array of all the items and we can do all the looping we want 
                    ...
kestler01 commented 1 year ago

would also be worth writing a custom promise using all

nabbott98 commented 1 year ago

Hmm ok, i'll look into it, utilizing the stripe is more important now, I'll close the issue and reopen when I get back to this step