[英]How can I insert values to an object property of an array?
The thing is that I don't know if there is something wrong with how I insert the data with the post or if the problem is with the HTML syntaxis of how I try to access the properties of an object property of an array.问题是我不知道我在帖子中插入数据的方式是否有问题,或者问题是否出在我尝试访问数组的 object 属性的语法的 HTML 语法上。
This is my model and I would like to insert for example a value in "cantidad" of "MaterialesProductos" array.这是我的 model,我想在“MaterialesProductos”数组的“cantidad”中插入一个值。
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const bodyParser = require('body-parser')
const ProductoSchema = new Schema({
IdProducto:{type:String},
MaterialesProductos:[{nombre:{type:String},cantidad:{type:Number}}],
precio:{type:Number},
image:{type:String},
nombre:{type:String},
descripcion:{type:String},
});
const Producto = mongoose.model('Producto',ProductoSchema);
module.exports = Producto;
This is my post where I insert all data with a "req.body".这是我的帖子,我在其中插入所有带有“req.body”的数据。 Always get the array empty.
总是让数组为空。
const Producto = require('../models/Productos.js')
const fileUpload = require('express-fileupload')
const path = require('path')
module.exports = (req,res)=>{
console.log(req.body)
let image = req.files.image;
image.mv(path.resolve(__dirname,'..','public/img',image.name),async (error)=>{
await Producto.create({
...req.body,
image: '/img/' + image.name
})
res.redirect('/AgregarProductos')
})
}
I've already tried with MaterialesProductos[].cantidad or MaterialesProductos[][cantidad] etc but I just can't insert the value.我已经尝试过使用 MaterialesProductos[].cantidad 或 MaterialesProductos[][cantidad] 等,但我无法插入值。
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<input type="button" name="abrirse" id="open" value="Agregar materiales">
<div id="popup" style="display: none;">
<div class="content-pop">
<div><a href="#" id="close">X</a></div>
<% for (var a = 0; a < materiales.length; a++) { %>
<div>
<%=materiales[a].Descripcion%>
<input type="number" value="0" name="MaterialesProductos.cantidad" min="0">
</div>
<% } %>
</div>
</div>
</div>
</div>
Well, I researched and didn't find a solution.好吧,我研究了但没有找到解决方案。 So I had to do it manually.
所以我不得不手动完成。 With "MaterialesProductos[nombre]" (can be anything) I get the values in an array with "req.body.MaterialesProductos[nombre]" I access to it.. Using "$push" (I can't insert or create it so I just can updateOne) I first create the document and after I update it adding the array with both objects.
使用“MaterialesProductos[nombre]”(可以是任何东西)我使用“req.body.MaterialesProductos[nombre]”获取数组中的值我访问它..使用“$push”(我无法插入或创建它所以我只能 updateOne) 我首先创建文档,然后在更新它之后添加包含两个对象的数组。
Something like this:像这样:
const Producto = require('../models/Productos.js')
const fileUpload = require('express-fileupload')
const path = require('path')
module.exports = (req,res)=>{
let image = req.files.image;
image.mv(path.resolve(__dirname,'..','public/img',image.name),async (error)=>{
await Producto.create({...req.body,
image: '/img/' + image.name
})
for (a=0; a<req.body['MaterialesProductos[cantidad]'].length;a++){
if (req.body['MaterialesProductos[cantidad]'][a]!=='0'){
await Producto.updateOne({nombre:req.body.nombre},{ $push: {MaterialesProductos: { nombre:req.body['MaterialesProductos[nombre]'][a] ,cantidad:req.body['MaterialesProductos[cantidad]'][a]} }})
}
}
res.redirect('/AgregarProductos')
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.