2017-04-11 4 views
0

HTMLページからノードJSサーバーに単純なフォームを送信しようとしています。問題は次のとおりです。私はユーザーがそれを必要とするフィールドを持っているので、配列を使って表現しています。すべてOKですが、配列フィールドはサーバー上のJSONオブジェクト内のStringのように見えます。私は一部アレイにアクセスすることはできません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]' 
} 

:ここ


は、サーバから出力されます。

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


サーバー側(ルート):

一部アレイは...


index.ejs(フォームとスクリプト)incresedすることができます

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; 


ありがとう!

答えて

1

要素の名前を変更します。それはあなたが、私はまだエラーを取得

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

ような配列を持つことになりますたよりpart[][]

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

でなければなりません。一重引用符はそのままです。私のサーバのオブジェクト 'model.part'にアクセスできません。私は 'undefined'を受け取るだけです。 –

+0

そして、モデルを印刷するときに 'part'と表示されますか? – Justinas

+0

はい。 'model = req.body'の場合私の投稿の出力例のように。今、 '' part [0] [nome] ':' PAULO H T GLINSKI ''。私は他のオブジェクトにアクセスすることができます。 –

関連する問題