简体   繁体   English

如何在Hapi中正确处理错误?

[英]How do I handle error properly in Hapi?

I started learning Hapi but I am unsure of error handling. 我开始学习Hapi,但是不确定错误处理。

Heres my code: 这是我的代码:

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

This will generate 这将产生

Unhandled rejection Error: reply interface called twice 未处理的拒绝错误:两次调用了答复接口

How do I handle validation error properly? 如何正确处理验证错误? I can't find many 我找不到很多

code samples about Hapi and giving me a hard time. 关于Hapi的代码示例,这给我带来了麻烦。

Thanks! 谢谢!

Why is there a 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); })  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM