javascript NodeJS - 如何打破一个功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12662930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS - how to break a function?
提问by f1nn
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!
Node.JS 告诉我所有的中断都是非法的。但是我应该如何以适当的方式打破我的功能?它不返回任何东西。谢谢!
回答by I Hate Lazy
A return
statement is used to halt a function.
一个return
声明来暂停功能。
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;
.
您可以提供一个可选的返回值,但在这种情况下,我相信它会被忽略,因此您应该能够替换break;
为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.
旁注,但是您有很多重复的代码,并且您的if
条件中有分配。您通常可以将重复因素考虑在内。此外,return
如果您使用if/else if/else
语句,您可以完全摆脱。
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 });
});
}
});