【问题标题】:Hapi AssertionError: Cannot set path parameters validations without path parameters: GETHapi AssertionError:无法在没有路径参数的情况下设置路径参数验证:GET
【发布时间】:2019-08-08 13:07:09
【问题描述】:

当我尝试迁移到 hapi v17 时发生这种情况..

这是我的终点

{ method: 'GET', path: '/admin/pagetypes', handler: Handlers.index, config: { description: 'get page types', tags: ['api'], auth: { strategy: 'standard', scope: 'admin' }, validate : { params : { p: Joi.number().default(0), message: Joi.string().empty(''), filter: Joi.string().empty(''), keyword: Joi.string().empty(''), by: Joi.string().empty(''), field: Joi.string().empty('') } } } },

当启动我的 hapi 服务器时,它说

{ AssertionError [ERR_ASSERTION]:无法设置没有路径参数的路径参数验证:GET /admin/pagetypes 在新的 module.exports.internals.Route (/Users/computer/Documents/proj/api/node_modules/hapi/lib/route.js:128:14)

尝试删除验证并运行,但这不是我想要的

【问题讨论】:

    标签: javascript reactjs hapijs


    【解决方案1】:

    您正在尝试验证路径参数,但您的路径定义中没有参数。

    这里:path: '/admin/pagetypes',

    我认为您正在尝试验证将添加到您的路径中的查询参数,如下所示:/admin/pagetypes?p=1&message=Hello 等。

    如果你是,试试这个。这将验证您的查询参数。

    {
        method: 'GET',
        path: '/admin/pagetypes',
        handler: Handlers.index,
        config: {
          description: 'get page types',
          tags: ['api'],
          auth: {
              strategy: 'standard',
              scope: 'admin'
          },
          validate : {
            query : {
              p: Joi.number().default(0),
              message: Joi.string().empty(''),
              filter: Joi.string().empty(''),
              keyword: Joi.string().empty(''),
              by: Joi.string().empty(''),
              field: Joi.string().empty('')
            }
          }
        }
      },
    

    要验证路径参数,您需要为路径定义设置一些动态值,例如

    path: '/admin/pagetypes/{pageId}',

    pageId 这里是一个路径参数然后你可以使用 validate.params 验证该参数。

    Here is the reference

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      相关资源
      最近更新 更多