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

问题描述:

我想从我的HTML页面发送一个简单的表单到我的节点JS服务器。问题是:我有一个需要由用户增加的字段,所以我使用数组来表示它。一切正常,但数组字段在服务器上的JSON对象内显示为一个字符串。发送HTML和节点JS之间的数组字段


下面是来自服务器的输出:

{ 
    nomeEquipe: 'Team', 
    nomeLider: 'Team Lider', 
    emailEquipe: '[email protected]', 
    matriculaLider: '10101010', 
    senhaLider: '001001001', 
    'part[0].nome': 'Partner', 
    'part[0].matricula': '666', 
    'part[0].email': '[email protected]' 
} 

我无法访问部分阵列。该部分阵列可以incresed ...


index.ejs(形式和脚本):

<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; 


非常感谢!

更改元素的命名。它应该是part[][]

<input type="email" name="part[0][email]"> 

比你有数组一样

{part:[ 
    0: { 
     nome: 'Partner', 
     matricula: '666', 
     email: '[email protected]' 
    } 
]} 
+0

我仍然得到错误。单引号仍然存在。我无法访问我的服务器中的对象'model.part'。我只收到'undefined'。 –

+0

打印出模型时,您会看到“零件”? – Justinas

+0

是的。在'model = req.body'的情况下。像我的文章中的输出例子。现在''part [0] [nome]':'PAULO H T GLINSKI''。我可以访问其他对象。 –