0

The users on my website can change their passwords.

The problem is that when the server produces an error in the process I have to set a warning message and say something like this:

'Warning, the password couldn't be changed, please try again!'

I made two routes in express

app.get('/editMode/:user',ensureAuthUser,routes.user.editMode);
app.get('/editMode/:user/:message',ensureAuthUser,routes.user.editMode);

The first shows the editMode template
The second is supposed to show the editMode and a message from the server

In the editMode.js

var warn=null;
    if(req.params.message) warn=validWarn(req.params.warn) ?JSON.parse(req.params.message) :null;
    console.log('message value: '+req.params.message);
    console.log('warn value: '+warn);

In both cases when I redirect to /editMode/+'[email protected]'+/+'warning message' or /editMode/+'[email protected]' I get

console.log(req.params.message)// value: undefined
console.log(warn)// value: null

Why can't I send a message to the user if my db can't change the password?

EDIT 2

editMode render

return res.render('templates/edit_template,
                            {
                                title: title,
                                usuario: req.session.passport.user,
                                warning: warn || null,
                            })

in the template edit_template.jade

-if(warning && warning.length)
                    h2 Error: #{warning}
                -else
                    h2 success
2
  • This looks very hackish, and I think you're actually trying to solve a different problem: that of showing a warning message when an error occurs. Does routes.user.editMode render a template? If so, why not show the message that way? Commented May 6, 2013 at 19:28
  • the template shows the edit mode... when i change the password i have to redirect to the edit mode again..., how can i show the message by that way? Commented May 6, 2013 at 19:35

1 Answer 1

1

Since you're using sessions, I would store the warning in the session:

if (theErrorOccurred)
{
  req.session.warning = 'this is your warning message';
}
else
{
  req.session.warning = null;
};
res.redirect('/editMode/...');

And pass it to your template in routes.user.editMode:

return res.render('templates/edit_template', {
  title   : title,
  usuario : req.session.passport.user,
  warning : req.session.warning
});

Also check out connect-flash as a more elaborate way of passing around different types of messages between requests.

Sign up to request clarification or add additional context in comments.

4 Comments

what happends with the other session' users
sessions are tied to one user only (req.session is unique and only accessible by that user).
i have a problem, i discover that when i redirect to the /editMode/ the console.log print two times... like when a process repeat two times... so i print the req.session.warning and before i set warning to null... the console print first print: [Object] second print: null but im redirecting one time
@andrescabana86 not entirely sure what you mean (which console.log?); perhaps it warrants a new question (with your new code)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.