Estoy teniendo un momento difícil para resolver este. pero antes mostraré primero mi controlador, ruta y esquema.
Aquí está mi controlador:
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' } }) }) }aquí está mi ruta:
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') })y el esquema:
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() } })cuando ejecuto una búsqueda de cartero, recibo este error
let oTotal = await User.findById(id).then(total => total.userOrders.subtotal + total.totalAmount) ^ TypeError: Cannot read properties of null (reading 'userOrders')Intenté buscar en Google cómo funcionan los objetos, pero sin solicitarlo al cuerpo, no puedo extraer el objeto "subtotal" (tengo valores para el subtotal) de "userOrders" para agregarlo a "TotalAmount". ¿Algun consejo?