我只想Joi
在Hapi
API中实现。
server.route([
{
method: 'POST',
path: '/login',
config: {
tags: ['login', 'auth'],
auth: false,
validate: {
payload: payloadValidator,
failAction: (req, h, source, error) => {
console.log("Error ::: ", source.details[0].message);
return h.response({ code: 0, message: source.details[0].message });
}
}
},
handler: async (request, h) => {
console.log(request.payload.email);
console.log(request.payload.password);
...
}
}
]);
听到我打电话payloadValidator
。
const payloadValidator = Joi.object({
email: Joi.string().required(),
password: Joi.string().required()
}).options({ allowUnknown: true });
实际上,我是新手,hapi
我的代码中缺少某些内容。谁能帮我解决此问题?
所需输出
如果我没有通过,email
则应用程序必须抛出的错误,Email is required
并且该password
字段也应相同。
错误:
Error ::: "email" is required Debug: internal, implementation, error Error: Lifecycle methods called before the handler can only return an error, a takeover response, or a continue signal at Request._lifecycle (/var/www/html/hapi/node_modules/@hapi/hapi/lib/request.js:326:33) at process._tickCallback (internal/process/next_tick.js:68:7)
作为错误提示,在处理程序只能返回错误,接管响应或必须返回接管响应的继续信号之前调用的Lifecycle方法。
return h.response({ code: 0, message: source.details[0].message }).takeover();
有关更多信息,您可以访问以下链接:参考链接
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句