无法设置标头node.js错误

时间:2016-06-15 16:36:14

标签: javascript json node.js

我知道它导致了多个res.json,但在这种情况下

router.post('/', function(req, res) {

  var data = {
    username: req.body.username,
    password: req.body.password
  };
Owner.getUser(data,function(err,response){
    if(err)
        throw err

    if(response){

        console.log(response.password);
        var createHash = function(password){
            return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
        };

        Owner.comparePassword(response.password, createHash(data.password) , function(err, result){

            if(result === true){

             res.json({msg: 1});

            }else{
              res.json({msg: 'Error, Incorrect password!'});
            }
        });

    }else{
        res.json({msg: 'No such account exists! '});
    }
  });

为什么我的逻辑可以说throw new Error('Can\'t set headers after they are sent.');因为错误而导致错误。每个if条件只运行一个res.json

0 个答案:

没有答案