Javascript 在HTML和节点JS之间发送数组字段

Javascript 在HTML和节点JS之间发送数组字段,javascript,html,node.js,string,object,Javascript,Html,Node.js,String,Object,我正在尝试将一个简单的表单从我的HTML页面发送到我的NodeJS服务器。问题是:我有一个字段需要用户增加,所以我使用数组来表示它。一切正常,但数组字段在服务器上我的JSON对象中显示为字符串 以下是服务器的输出: 我无法访问部件阵列。零件数组可以增加 index.ejs表单和脚本: 非常感谢 更改元素的命名。它应该是第[]部分 我还是有错误。单引号仍然存在。我无法访问服务器中的对象模型.part。我收到的只是未定义的。当打印出模型时,你们看到零件了吗?是的。在情况下,model=req.

我正在尝试将一个简单的表单从我的HTML页面发送到我的NodeJS服务器。问题是:我有一个字段需要用户增加,所以我使用数组来表示它。一切正常,但数组字段在服务器上我的JSON对象中显示为字符串

以下是服务器的输出:

我无法访问部件阵列。零件数组可以增加

index.ejs表单和脚本:


非常感谢

更改元素的命名。它应该是第[]部分


我还是有错误。单引号仍然存在。我无法访问服务器中的对象模型.part。我收到的只是未定义的。当打印出模型时,你们看到零件了吗?是的。在情况下,model=req.body。就像我文章中的输出示例一样。现在是“第[0][nome]部分”:“保罗·格林斯基”。我可以访问其他对象。您使用的工具有问题,因为in将在GET请求中生成数组,但您的服务器将其读取为单级密钥。谢谢您的警告。默认情况下,app.js文件中的这一行存在问题:app.usebodyParser.urlencoded{extended:true};。我猜默认情况下,Express使用extended:false,并且必须为true才能工作。再次感谢,通过HTML中的chenges和服务器配置,您解决了我的问题;D
{ 
    nomeEquipe: 'Team',
    nomeLider: 'Team Lider',
    emailEquipe: 'email@email.com',
    matriculaLider: '10101010',
    senhaLider: '001001001',
    'part[0].nome': 'Partner',
    'part[0].matricula': '666',
    'part[0].email': '666@email.com'
}
    <form method="post" action="/cadastrar">
        <input type="text" name="nomeEquipe" placeholder="Nome da Equipe"><br>
        <input type="text" name="nomeLider" placeholder="Lider da Equipe"><br>
        <input type="email" name="emailEquipe" placeholder="Email do Lider"><br>
        <input type="number" name="matriculaLider" placeholder="Matricula do Lider"><br>
        <input type="password" name="senhaLider" placeholder="Senha de Login"><br><br>
        <input type="text" name="part[0].nome" placeholder="Nome do participante">
        <input type="number" name="part[0].matricula" placeholder="Matricula do participante">
        <input type="email" name="part[0].email" placeholder="Email do participante">

        <div id="participante"></div>
        <br>
        <button type="button" onclick="addParticipante()">Adicionar</button>
        <button type="button" onclick="delParticipante()">Remover</button>


        <br><br>
        <button type="submit">Cadastrar</button>
    </form>

<script>
        var cont = 1;

        function addParticipante() {
                var div = document.createElement('div');
                div.className = 'participante';
                div.innerHTML = '<input type="text" name="part['+cont+'].nome" placeholder="Nome do participante"><input type="number" name="part['+cont+'].matricula" placeholder="Matricula do participante"><input type="email" name="part['+cont+'].email" placeholder="Email do participante">';

                document.getElementById('participante').appendChild(div);
                cont++
        }

        function delParticipante() {
                var select = document.getElementById('participante');
                document.getElementById('participante').removeChild(select.lastChild);
                cont--
        }
</script>
var express = require('express');
var router = express.Router();

var equipe = require('./../models/Equipe')();

router.get('/', function(req, res, next) {
    res.render('index', { title: 'Gincana' });
});

router.get('/cadastrar', (req, res, next) => {
    equipe.find({}, (err, models) => {
        if(err) console.log(err)
        else        res.json(models)
    });
});

router.post('/cadastrar', validador, (req, res, next) => {
    var model = req.body;
    res.send(model);
});

function validador(req, res, next) {
   var model = req.body;

   console.log(model);
   next()
}

module.exports = router;
<input type="email" name="part[0][email]">
{part:[
    0: {
        nome: 'Partner',
        matricula: '666',
        email: '666@email.com'
    }
]}