0

I'm having a difficult time on solving this one. but before that I will show first my controller, route and schema.

Here is my controller:

module.exports.checkoutOrder = async (data) =>{

    let id = data.userId;
    

    let oTotal = await User.findById(id).then(total => total.userOrders.subtotal + total.totalAmount)

    User.findById(id).then(user=>{

        user.totalAmount.push({total,oTotal})
        return user.save().then((savedtotal,err)=>{
            if (savedtotal) {
                return savedtotal

            } else {

                return 'Failed to checkout order. Please try again'

            }

        })

})
}

here is my route:

route.get('/checkout',auth.verify,(req,res)=>{
    let token = req.headers.authorization;
    let payload = auth.decode(token)
    let isAdmin = payload.isAdmin
    let id = payload.id
    !isAdmin ? controller.checkoutOrder(id).then(result => 
        res.send(result)) 
    : res.send('Unauthorized User')

})

and the schema:

userOrders:[
            {
                productId:{
                    type: String,
                    required: [true, "ProductId is required"]
                },

                quantity:{
                    type: Number,
                    required: [true, "Quantity is required"]
                },

                subtotal:{
                    type: Number,
                    required: [true, "subtotal is required"]
                }
            }
        ],

    
                totalAmount:{
                    type: Number,
                    required: [true, "Quantity is required"]
                },

                PurchasedOn:{
                    type: Number,
                    default:  new Date()
                }
        
})

when i run a postman get, i receive this error

    let oTotal = await User.findById(id).then(total => total.userOrders.subtotal + total.totalAmount)
                                                             ^

TypeError: Cannot read properties of null (reading 'userOrders')

I've tried googling how objects work but without requesting from body, I cannot pull "subtotal"(i have values for subtotal) object from "userOrders" to add it to "TotalAmount". Any tips?

  • You seem to be mixing continuation passing (`.then(...)`) with async (`await ...`), also you seem to be confused with the entities on your repository (which model is `User.findById` supposed to return?). Also, your schema is wrongly copied (I suppose, because the syntax doesn't match). Try to fix/clarify those points, so we can help you – Rodrigo Rodrigues Feb 20 '22 at 23:18
  • i used `User.findById` so i can access the schema User (i think this is the only method i know in calling a schema). in await part. i just want to add the subtotal on the user order to totalAmount. I tried removing await on my create order (https://stackoverflow.com/questions/71198649/how-to-push-an-object-coming-from-postman-as-a-numbervalidationerror) and it comes with an error: promise pending. i think im a bit confused because someone thought me await's purpose is to do the promise immediately – exekiel vergara Feb 20 '22 at 23:37
  • await will make your code release the thread until the promise is resolved, then it will capture the returned value and bind it to the variable (if there is an assignment). You can then use that in another statement with the value bound, not by nesting the next operations inside a function in `.then` – Rodrigo Rodrigues Feb 22 '22 at 22:52

0 Answers0