Patricio Carvajal H.
Patricio Carvajal H.

Reputation: 361

Node+Express Post Error

I am trying to post fields from the form but the lista[0].usuario seems to be empty in the jade page.

I am 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');

// all 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 only
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

Upvotes: 0

Views: 400

Answers (3)

Wilson
Wilson

Reputation: 9136

You are calling the 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 you see, you don't have any declaration of your function in the login.js file.

You only have the login exposed on the exports object:

exports.login = function(req, res){
    res.render('login');
};

Like the above post says, you can declare your login function as separate function:

function login(req, res){
    res.render('login');
}

and also update the 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 would work ok.

It would be something like this: https://gist.github.com/wilsonbalderrama/d84cc800efb1169fea81


UPDATES:

  • You are using Express 3.X

Regarding your another problem you need to add the emphasized line to your app.js file:

    // all environments
    ..
    app.use(express.logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded());
    ..

Then you will be able to get the values usuario and password in your req.body object.


ANOTHER UPDATE:

You need to check if the lista variable already contains the username and password. Change the emphasized code to index.jade:

      ..
      input(type='submit', value='Conectar')
      br
      br

    if lista
      h1 #{lista[0].usuario}

    #footer
    ..

Once you finish processing usuario and password values, you need to re-render your index html passing the list array:

Then in your exports.post_enviar method change the emphasized code at the end of the method:

      ..
      lista.push({
          usuario: usuario,
          password: password
      })   

      console.log(lista[0].usuario);
      console.log(lista[0].password);  

      res.render('index', {lista: lista});
    }

Upvotes: 1

Sachacr
Sachacr

Reputation: 732

You push only one element in your array lista so :

lista[1] does not exist. 

To get the password you should try :

lista[0].password;

Upvotes: -1

Sachacr
Sachacr

Reputation: 732

Your function login is exported but not named. You should name it like this :

exports.login = function login(req, res){
    res.render('login');
};

Or you can call your login method like this : exports.login(req, res);

You can also delcare your function and export it separately :

function login(req, res){
    res.render('login');
};

exports.login = login;

Upvotes: 1

Related Questions