Closed nabbott98 closed 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
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
...
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
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:
What is the actual behavior? A clear and concise description of what actually happened.
Post any code you think might be relevant (one fenced block per file)
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