javascript - Node+Express Post Error -
i trying post fields form lista[0].usuario seems empty in jade page.
i using: node version v0.12.3000 express 3.20.3
--my code--
---app.js---
var express = require('express'); var routes = require('./routes'); var http = require('http'); var path = require('path'); var app = express(); var v_login = require('./routes/login'); // environments app.set('port', process.env.port || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development if ('development' == app.get('env')) { app.use(express.errorhandler()); } app.get('/login', v_login.login); app.get('/login', v_login.get_enviar); app.post('/login', v_login.post_enviar); http.createserver(app).listen(app.get('port'), function(){ console.log('express server listening on port ' + app.get('port')); });
---login.js---
var lista = new array(); function login(req, res){ res.render('login'); }; exports.login = login; exports.get_enviar = function(req, res){ login(req, res); } exports.post_enviar = function(req, res){ var usuario = req.body.usuario; var password = req.body.password; console.log(usuario); console.log(password); lista.push({ usuario: usuario, password: password }) res.render('login', {lista: lista}); console.log(lista[0].usuario); console.log(lista[0].password); }
---login.jade---
doctype html html(lang='en') head body #container form#frm(method="post", name="frm", action="login", enctype="application/x-www-form-urlencoded") h1 login h2 usuario input#usuario(name='usuario', type='text', value='') br h2 password input#password(type='password', name='password', value='') br br input(type='submit', value='conectar') br br if lista h1 #{lista[0].usuario} #footer
you calling login function:
exports.post_enviar = function(req, res){ var usuario = req.body.usuario; var password = req.body.password; lista.push({ usuario: usuario, password: password }) login(req, res); // here }
but if see, don't have declaration of function in login.js file.
you have login exposed on exports object:
exports.login = function(req, res){ res.render('login'); };
like above post says, can declare login function separate function:
function login(req, res){ res.render('login'); }
and update export object to:
exports.login = login;
so that:
exports.post_enviar = function(req, res){ var usuario = req.body.usuario; var password = req.body.password; lista.push({ usuario: usuario, password: password }) login(req, res); // here }
it work ok.
it this: https://gist.github.com/wilsonbalderrama/d84cc800efb1169fea81
updates:
- you using express 3.x
regarding problem need add emphasized line app.js file:
// environments .. app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); ..
then able values usuario , password in req.body object.
another update:
you need check if lista variable contains username , password. change emphasized code index.jade:
.. input(type='submit', value='conectar') br br if lista h1 #{lista[0].usuario} #footer ..
once finish processing usuario , password values, need re-render index html passing list array:
then in exports.post_enviar method change emphasized code @ end of method:
.. lista.push({ usuario: usuario, password: password }) console.log(lista[0].usuario); console.log(lista[0].password); res.render('index', {lista: lista}); }
Comments
Post a Comment