Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS-如何中断函数?_Javascript_Node.js_Callback_Return_Break - Fatal编程技术网

Javascript NodeJS-如何中断函数?

Javascript NodeJS-如何中断函数?,javascript,node.js,callback,return,break,Javascript,Node.js,Callback,Return,Break,最近有一个类似的问题,但仍然无法理解。在添加新用户之前,我必须验证注册页面 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', { "t

最近有一个类似的问题,但仍然无法理解。在添加新用户之前,我必须验证注册页面

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告诉我所有的中断都是非法的。但是我应该如何以适当的方式破坏我的函数呢?它不会返回任何东西。谢谢

一个
return
语句用于停止函数

您可以提供一个可选的返回值,但在这种情况下,我相信它会被忽略,所以您应该能够替换
break
返回


旁注,但您有大量重复代码,并且在
if
条件中有赋值。你通常可以排除重复。此外,如果使用
if/else if/else
语句,则可以完全去掉
return

这里有一个例子

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 });
        });
    }
});


你是说类似smth的返回空值吗?@IlyaRusanen:Just
return应该可以做到这一点。不需要提供显式返回值。