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

372
Views
Cuando intento insertar un _id en una matriz de ObjectId, Typescript arroja un error

Este es el código:

 async function create(data: any) { try { //Creates and order let createdOrder = await model.create(data); //Push into bike orders array the id of the newly created order let bike = await bikeModel.findById(createdOrder._id); bike?.orders.push(createdOrder._id); } catch (error: any) { console.log(`[${RESOURCE}:create] controller error:${error}`); throw error; } }

Este es el error que me sale:

 Argument of type 'ObjectId' is not assignable to parameter of type '{ type: typeof mongoose.Types.ObjectId; ref: "Order"; }'. Type 'ObjectId' is missing the following properties from type '{ type: typeof ObjectId; ref: "Order"; }': type, refts(2345)

Y este es mi modelo:

 const bikeSchema = new mongoose.Schema({ name: String, model: String, color: String, location: String, rating: String, available: String, orders: [ { type: mongoose.Types.ObjectId, ref: 'Order', }, ],

});

Intenté casi todo, pero parece que no me deja hacer nada.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema es que está tratando de insertar un valor único en una matriz de objetos de la forma correcta. Si observa su esquema, notará que el atributo de sus pedidos es del tipo:

 { type: mongoose.Types.ObjectId, ref: 'Order' }[]

Por lo tanto, su código debería verse de la siguiente manera:

 async function create(data: any) { try { //Creates and order let createdOrder = await model.create(data); //Push into bike orders array the id of the newly created order let bike = await bikeModel.findById(createdOrder._id); bike?.orders.push({type: createdOrder._id, ref: 'order'}); } catch (error: any) { console.log(`[${RESOURCE}:create] controller error:${error}`); throw error; } }
about 4 years ago · Juan Pablo Isaza Report
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!