Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
¿Cómo agrego un objeto anidado a un objeto en el mismo esquema?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!