node.js - 如何在 Hapi 中正确处理错误?

标签 node.js rest error-handling sequelize.js hapijs

我开始学习 Hapi,但我不确定错误处理。

这是我的代码:

    method: ['POST'],
    path: '/users',
    handler: function (request, reply) {
        if (request.query['password'] == request.query['password_confirmation']) {
            models.User.create({
                'email': request.query['email'],
                'password': request.query['password'],
                'password_confirmation': request.query['password_confirmation'],
            })
                .catch(models.Sequelize.ValidationError, err => {
                    return reply(err);
                })
                .then(user => {
                    return reply(user);
                });
        } else {
            return reply('password do not match');
        }

这将产生

未处理的拒绝错误:回复接口(interface)调用了两次

如何正确处理验证错误?我找不到很多

关于 Hapi 的代码示例,让我很难受。

谢谢!

最佳答案

为什么会有一个 then block post catch

 models.User.create({ 
 'email':request.query['email'], 
 'password': request.query['password'],
   'password_confirmation:request.query['password_confirmation'],
}) 
.then(user => { return reply(user); })
.catch(models.Sequelize.ValidationError, err =>
{ return reply(err); })  

关于node.js - 如何在 Hapi 中正确处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44857410/

相关文章:

node.js - fs.readdirSync 卡住 Electron 应用程序

node.js - 在 MongoDB 中,更好的做法是为常用查询对象提供它们自己的引用模式,还是将它们保留在其父级中?

node.js - 应用架构应该如何构建?

ios - 如何解决这个问题 "Ambiguous reference to member ' dataTask(with :completionHandler: )' "

c# - 如何在 C# 中编写 REST Get-Request?

node.js - 流上的 ECONNREFUSED 错误未由错误事件处理并 try catch

vba - 除了自定义错误处理程序之外,如何在使用默认错误处理程序时避免级联

symfony - 如何修复Symfony中 Composer 更新后找不到的类?

javascript - ES6 解构的 promise ?

.net - 如何解决 "InvalidOperationException:operation must have a single parameter whose type is Stream"