简体   繁体   English

如何将值插入数组的 object 属性?

[英]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')
    })

  


    }

And worked.并且工作了。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对象数组中插入 object - How can I insert an object in an array of objects 如何将对象属性值与数组值进行比较,然后将对象值替换为数组值? - How do I compare an object property values to the values of an array, and then replace the object values with the array values? 如何从JSON返回嵌套在对象中的嵌套JavaScript数组的属性值? - How can I return the property values of a nested JavaScript array nested in an object from JSON? 如何使用数组值过滤对象? - How can I filter the Object with an Array values? 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 如何使用jquery / javascript将值插入javascript对象 - How can I insert values in to javascript object by using jquery / javascript 如何从 object 中删除属性但保留其值? - How can i remove a property from an object but keep its values? JavaScript:如何将对象插入到对象数组中? - JavaScript: How can I insert an Object into an Array of Objects? 如何获取所有对象的属性并将它们插入到自己的对象数组属性中? - How do I take all of an object's properties and insert them into its own object array property? 如何通过属性值在 JSON 数组中累积值? - How can i accumulate values in a JSON Array by the value of a property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM