承诺可取消的蓝鸟

时间:2016-01-03 08:56:27

标签: javascript node.js promise bluebird cancellation

我在快递中有注册功能

app.post('/api/signup', function(req, res) {
  var username = req.body.username;
  var password = req.body.password;
  if (!username || !password) {
    return res.status(400).end();
  }
  var promiseObject = userModel
    .findOne({username: username})
    .then(function(user) {
      if (user) {
        console.log(user);
        res.json({success: false, message: 'User or Email already exists !'});
        throw new promise.CancellationError()
      } else {
        var newUser = {username: username, password: bcrypt.hashSync(password)};
        return userModel.create(newUser)
      }
    })
    .cancellable()
    .catch(promise.CancellationError, function(e) {
      console.log(e.msg);
    })
    .then(function(user) {
      console.log('User created successfully');
      res.json({success: true});
    })
    .catch(function(err) {
      console.log(err);
    })
});

当我从邮递员调用函数时,显示错误:未处理的拒绝错误,发送后无法设置标题

0 个答案:

没有答案