0

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

3 Answers3

1

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;
Sachacr
  • 704
  • 1
  • 9
  • 17
1

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});
    }
Wilson
  • 9,006
  • 3
  • 42
  • 46
  • @PatricioCarvajalH. you can see this answer for you another problem http://stackoverflow.com/questions/7522034/node-js-express-form-post-req-body-not-working you are not getting the values from your html form at the express.js in your req.body – Wilson Jun 04 '15 at 14:10
  • Wilson, perfect! but still can't see the value in the login.jade file (lista[0].usuario) – Patricio Carvajal H. Jun 04 '15 at 15:44
  • I added the enctype tag but still can see the value of lista[0].usuario in the jade file, the above code is updated – Patricio Carvajal H. Jun 04 '15 at 17:13
  • works first time but second time got the error: Error: Can't set headers after they are sent. – Patricio Carvajal H. Jun 04 '15 at 19:30
  • @PatricioCarvajalH. you need to stop calling __login function__ inside __post_enviar method__, since I modified your code to call directly __res.render__ for then passing by the __array lista__ . – Wilson Jun 04 '15 at 19:36
  • sorry I missed that part, now I don't get the error, but second and later times keep printing the first "usuario" I send. For example: Try 1. user "aaaa" --> submit --> login.js prints "aaa" at the end of the page, then if you enter another user "bbb" and click the submit button, you get printed "aaaa" instead of "bbbb". – Patricio Carvajal H. Jun 04 '15 at 19:56
  • @PatricioCarvajalH. you are using an array called list and while the server is not restarted, that variable is going to store items of sent by the form as many times as you hit the submit button but if you see the logic of the code you will reliaze that it is always grabbing the first item that was pushed: __list[0]__.usuario – Wilson Jun 04 '15 at 20:06
  • oic, so, is there any other way to get the last usuario? this has to be done with an array? – Patricio Carvajal H. Jun 04 '15 at 20:27
  • well you could do something like in this gist:https://gist.github.com/wilsonbalderrama/1fd72bfc5165ca763768 – Wilson Jun 04 '15 at 20:43
-1

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;
Sachacr
  • 704
  • 1
  • 9
  • 17