Node.js - Get input values?

Asked

Viewed 2,858 times

3

There I go again. I have a function where a registration in the Mysql database is working. But while trying to get the data from an input I get the following error:

Typeerror: Cannot read Property 'user' of Undefined

Node:

exports.AddUser = function(request, reponse){
    console.log('Email: ', reponse.body.user.email);
    Model.addUser({email_usuario: Math.random() , senha_usuario:'sds' }, function(user){
        reponse.render('user/add', {
            title: 'cadastro'
        });
    }); 
};

Jade:

extends ../layout
block content
form(action="../user/add", method="post")
    label Email
    input(type="text" name="user[email]")
    label senha
    input(type="password" name="user[senha]")
    button(type="submit") Cadastrar
br

I’ve tried with reponse.body.email , input(type="text" name="senha") and it didn’t work, what’s wrong here?

  • 1

    On what line you make that mistake?

  • 3

    Instead of reponse.body.email, use the request.body.email

  • Hey guys, I was able to solve it and I just had to put the 'body-parser' module and put this 'app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ Extended: true }));'

1 answer

2


var express = require("express"),
    http = require("http"),
    path = require("path"),
    bodyParser = require('body-parser');

app = express();

app.set('port', 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(express.static(path.join(__dirname, 'static')));


require('./router')(app);

http.createServer(app).listen(app.get('port'), function(){
   console.log('Running');
});

I just added the 'body-parser' module and added this excerpt

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));


   console.log('Email: ', request.body.nome);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.