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.
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')) });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