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

337
Views
Expresar mongoose poblar matriz de subdocumentos de POST

Este es mi esquema de mangosta:

 const InvoiceSchema = new Schema({ name: { type: String, required: true }, description: { type: String }, items: [{ product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'}, amount: { type: Number }, name: { type: String, required: true }, quantity: { type: Number }, rate: { type: Number, required: true } }], createdBy: { type: Schema.ObjectId, ref: 'User', required: true }, }

Ahora quiero completar mi esquema a partir de datos POST. Mi problema es que no sé cómo publicar mis elementos (¿cómo nombro mis campos)?

Uso PostMan para publicar datos.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Para obtener datos de la publicación

Para agregar un nuevo registro en mangosta

 const {ObjectId} = mongoose.Schema.Types; const newInvoice = new InvoiceSchema({ name: "John Smith", description: "This is a description", items: [{ product: 'THIS_IS_AN_OBJECT_ID_STRINGIFIED', amount: 2, quantity: 5, //name - comes from the product model //rate - comes from the product model }] }); newInvoice.save();

Para POST y guardarlo

 //Response format { name: 'John Smith', description: 'This is a description', items: [ { product: 'THIS_IS_AN_OBJECT_ID', amount: 2, quantity: 5 } ] } app.post('/yourRoute', (req, res) => { const {name, description, items} = req.body; const newInvoice = new InvoiceSchema({name, description, items}); newInvoice.save().then(()=>res.send('success')) });
about 4 years ago · Santiago Trujillo Report

0

Para agregar elementos de forma masiva

 const invoice = new Invoice(); invoice.items = req.body.items;

Para agregar un solo artículo

 invoice.items.push(item);

Para actualizar un solo elemento

 const item = invoice.items.id(req.params._id); item.attribute = ... // Do update
about 4 years ago · Santiago Trujillo 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!