mercredi 25 février 2015

Why can't I use res.redirect twice?


create: function(req, res, next) {

// Check for email and password in params sent via the form, if none
// redirect the browser back to the sign-in form.
console.log(req.param('name'));


if (!req.param('email') || !req.param('password')) {
var usernamePasswordRequiredError = [{
name: 'usernamePasswordRequired',
message: 'You must enter both a username and password.'
}]

// Remember that err is the object being passed down (a.k.a. flash.err), whose value is another object with
// the key of usernamePasswordRequiredError
req.session.flash = {
err: usernamePasswordRequiredError
}

res.redirect('/session/new');
return;

}

User.findOne({name:req.param('name'),encryptedPassword:req.param('encryptedPassword')}).exec(function foundUser(err, user) {

if (!user) return next(new Error('User not found.'));



console.log("auth this user");

// Log user in
req.session.authenticated = true;
req.session.User = user;

res.redirect('/user/show/'+user.id);



});

}


In this code res.redirect won't work. It will if I comment the first one. I got an error, searched for it and found out this was the issue but I don't understand why I can't do that? I'm learning sails with sailscast and the guy seems to do that a lot. See here: http://ift.tt/1JMfPni my code is based on this


Aucun commentaire:

Enregistrer un commentaire