【问题标题】:How do I handle error properly in Hapi?如何在 Hapi 中正确处理错误?
【发布时间】:2017-12-05 01:26:39
【问题描述】:

我开始学习 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');
        }

这会生成

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

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

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    为什么会有 then 块 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); })  
    

    【讨论】:

    • 哎呀,你是对的。我不确定承诺的语法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2012-12-28
    • 2010-10-11
    • 1970-01-01
    相关资源
    最近更新 更多