Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
Node.js 为什么我会得到;错误:Can';t在发送后设置标题";尽管发送了状态码?_Node.js_Http_Express_Passport.js - Fatal编程技术网

Node.js 为什么我会得到;错误:Can';t在发送后设置标题";尽管发送了状态码?

Node.js 为什么我会得到;错误:Can';t在发送后设置标题";尽管发送了状态码?,node.js,http,express,passport.js,Node.js,Http,Express,Passport.js,这是我的密码: app.post('/register', function(req, res){ var user = new User(req.body); if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); } if (req.body.username.length <= 3){ res.status(400).send('error

这是我的密码:

app.post('/register', function(req, res){
  var user = new User(req.body);
  if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); }
  if (req.body.username.length <= 3){ res.status(400).send('error: username must be longer'); }
  User.findOne({
    username: req.body.username
  }, function(err, userr){
       if(err) {res.status(400).send(err); }
       if (userr){res.status(400).send('error: user already exists'); }
       user.save(function(err, user){
         if (err){ res.status(400).send('couldn\tt save fo sum rezon'); }              
         if (user) { res.send(user); }
       });
    });
});
我不知道我在哪里发送了不止一次的头?这段代码是否应该在满足其中一个条件后立即停止执行,或者如果不满足任何条件,则只渲染用户


如果有人能给我提供资源,让我了解express routing如何工作的细节,我将获得额外的积分

以下面这行为例:

if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); }
这样,只有当两个
都失败时,才会对其余代码进行计算。

添加到

如果在您的示例中:

 if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); }
  if (req.body.username.length <= 3){ res.status(400).send('error: username must be longer'); }

类似于这个问题(但不是完全重复):非常好,谢谢你,约翰。我应该总是在回复时添加回报吗?不总是。。仅当您有条件地发送响应时
  ...
  var user = new User(req.body);
  if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); }
  else if (req.body.username.length <= 3){ res.status(400).send('error: username must be longer'); } 
  else { // include the rest of your function code here... }
 if (req.body.password.length <= 5){ res.status(400).send('error: password must be longer'); }
  if (req.body.username.length <= 3){ res.status(400).send('error: username must be longer'); }
if (req.body.password.length <= 5){ 
    res.status(400).send('error: password must be longer'); 
  } else if (req.body.username.length <= 3){ 
    res.status(400).send('error: username must be longer'); 
  }