Tuesday, 11 April 2017

Sending array field between HTML and Node JS

I'm trying to send a simple form from my HTML page to my Node JS server. The problem is that: I have a field that needs to be incresed by the user, so I'm using an array to express it. Everything OK but the array field appears like a String inside my JSON Object on the Server.


Here is the output from the Server:

{ 
    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'
}

I can't access the part Array. The part Array can be incresed...


index.ejs (The form and the script):

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


The Server side (Routes):

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;


Thanks a lot!



via Paulo Tokarski Glinski

No comments:

Post a Comment