我目前正在使用 hapi,我遇到了一个问题,我似乎找不到任何解决方案或以前提到过。当我发送以下请求时,只有我的第一个查询参数在 request.query 对象中。
curl -H "Content-Type: application/json" -X PUT https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id'
将 {}
和 ''
中的项目替换为实际名称。
我的路线目前是这样写的
server.route({
method: 'PUT',
path: '/lists/{username}/{listname}',
handler: function(request, reply) {
const username = encodeURIComponent(request.params.username);
const listname = encodeURIComponent(request.params.listname);
const resource_id = request.query.resource_id;
const token = request.query.token;
console.log(request.query);
verify(token, username, {callback}, listname, resource_id, reply);
}
});
console.log
调用结果为
{ token: 'token' }
如果我执行 console.log(resource_id)
,我会在控制台中得到“undefined”。 hapi 的文档声明所有查询参数都应在 request.query
对象中找到。由于某种原因,这没有发生。我查看了 hapijs 文档,查看了我的 API 调用,还阅读了人们处理查询参数的示例。知道这里发生了什么吗?
最佳答案
问题出在你的 curl 命令上,而不是 HapiJS。
尝试运行
curl -H "Content-Type: application/json"-X PUT "https://localhost:3000/lists/{username}/{listname}?token=token&resource_id=resource_id"
它是查询字符串中被解释为命令结尾的符号。有关命令为何不起作用的解释,请参阅 this questions answer
关于javascript - Hapijs 查询参数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198242/