【问题标题】:ValidationError: "value" must be an object - ResponseValidation phaseValidationError: "value" 必须是一个对象 - ResponseValidation 阶段
【发布时间】:2020-09-01 22:39:55
【问题描述】:

使用 hapi 服务器,我从这样的处理程序返回:

async function handler (req, h) {
  ...
  h.unstate(key, cookieOptions);
  const resp = h.redirect(redirectUri);
  return resp;
}

...但返回 HTTP 500 错误。我在堆栈跟踪中发现了失败:

ValidationError: "value" must be an object
    at Object.exports.process (~/project/node_modules/hapi/node_modules/joi/lib/errors.js:201:19)
    at internals.Object._validateWithOptions (~/project/node_modules/hapi/node_modules/joi/lib/types/any/index.js:751:31)
    at module.exports.internals.Any.root.validate (~/project/node_modules/hapi/node_modules/joi/lib/index.js:146:23)
    at exports.response (~/project/node_modules/hapi/lib/validation.js:173:31)
    at Request._postCycle (~/project/node_modules/hapi/lib/request.js:356:68)
    at Request._reply (~/project/node_modules/hapi/lib/request.js:335:20)
    at Request._execute (~/project/node_modules/hapi/lib/request.js:171:14)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

...这似乎来自 onPostHandler 和 onPreResponse 之间的响应验证生命周期步骤。

可能是什么问题?

【问题讨论】:

  • 也许redirectUri 是undefined 你试过h.response().redirect() 合成器吗?你能判断它是来自h.unstate() 还是真的来自重定向?
  • 谢谢@ErnestJones,我可以确认它来自重定向,h.response().redirect() 似乎按预期工作。

标签: node.js hapijs joi


【解决方案1】:

失败来自重定向时未初始化的响应,首先创建空响应应该修复它:

async function handler (req, h) {
  ...
  const resp = h.response().redirect(redirectUri);
  return resp;
}

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2020-01-01
    • 2019-06-23
    • 1970-01-01
    • 2018-04-14
    • 2013-12-08
    相关资源
    最近更新 更多