Recently had a similar question, but still can't get it. I have to validate registration page before adding new user.
app.post('/signup', function(req, res) {
//checking if first name is filled
if (req.body.first_name = "" || req.body.first_name = null || req.body.first_name = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_first_name" });
break;
}
//checking if last name is filled
if (req.body.last_name = "" || req.body.last_name = null || req.body.last_name = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_last_name" });
break;
}
//checking if email is filled
if (req.body.email = "" || req.body.email = null || req.body.email = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_email" });
break;
}
//checking if passwords match
if (req.body.password != req.body.repassword) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "pass_missmatch" });
break;
}
...
...
...
//and finally if everything seems to be OK...
addUser(req.body.email, req.body.password, req.body.first_name, req.body.last_name, req.body.country, function(status) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "success" : 1 });
});
});
Node.JS tells me that all the breaks are illegial. But how should I break my function in a proper way? It doesn't return anything. Thanks!
Recently had a similar question, but still can't get it. I have to validate registration page before adding new user.
app.post('/signup', function(req, res) {
//checking if first name is filled
if (req.body.first_name = "" || req.body.first_name = null || req.body.first_name = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_first_name" });
break;
}
//checking if last name is filled
if (req.body.last_name = "" || req.body.last_name = null || req.body.last_name = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_last_name" });
break;
}
//checking if email is filled
if (req.body.email = "" || req.body.email = null || req.body.email = undefined) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "empty_email" });
break;
}
//checking if passwords match
if (req.body.password != req.body.repassword) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : "pass_missmatch" });
break;
}
...
...
...
//and finally if everything seems to be OK...
addUser(req.body.email, req.body.password, req.body.first_name, req.body.last_name, req.body.country, function(status) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "success" : 1 });
});
});
Node.JS tells me that all the breaks are illegial. But how should I break my function in a proper way? It doesn't return anything. Thanks!
Share Improve this question asked Sep 30, 2012 at 16:32 f1nnf1nn 7,04724 gold badges71 silver badges94 bronze badges1 Answer
Reset to default 13A return
statement is used to halt a function.
You can provide an optional return value, but in this case, I believe it would be ignored, so you should just be able to replace break;
with return;
.
Side note, but you have a good bit of repeating code, and you have assignments in your if
conditions. You can usually factor away the repetition. Also, you can get rid of the return
altogether if you use if/else if/else
statements.
Here's an example.
function isEmpty(val) {
return val === "" || val == null;
}
function renderWithError(req, res, msg) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "error" : msg });
}
app.post('/signup', function(req, res) {
if (isEmpty(req.body.first_name)) {
renderWithError(req, res, "empty_first_name");
}
else if (isEmpty(req.body.last_name)) {
renderWithError(req, res, "empty_last_name");
}
else if (isEmpty(req.body.email)) {
renderWithError(req, res, "empty_email");
}
else if (req.body.password != req.body.repassword) {
renderWithError(req, res, "pass_missmatch");
}
...
...
...
else {
addUser(req.body.email, req.body.password, req.body.first_name, req.body.last_name, req.body.country, function(status) {
res.render('signup', { "title": "Ttitle", "menu": "signup", user: req.user, "success" : 1 });
});
}
});