【问题标题】:Hapijs query parameters undefinedHapijs 查询参数未定义
【发布时间】:2023-03-17 23:05:02
【问题描述】:

我目前正在使用 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),我会在控制台中得到“未定义”。 hapi 的文档指出所有查询参数都应该在request.query 对象中找到。由于某种原因,这没有发生。我查看了 hapijs 文档,查看了我的 API 调用,并阅读了人们处理查询参数的示例。知道这里发生了什么吗?

【问题讨论】:

    标签: javascript node.js https hapijs query-parameters


    【解决方案1】:

    问题在于您的 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

    【讨论】:

    • 谢谢!踢自己,我忘记了 shell 看到“&”并在后台运行该进程。
    【解决方案2】:

    问题在于触发 curl 请求。 curl请求中有&,并且URL不在引号中;所以& 变成了一个修改后的shell 来启动一个分离的进程。您的路线运行良好,只需使用引号中的 URL 触发 curl

    curl -H "Content-Type: application/json" -X PUT "https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id'"

    【讨论】:

      【解决方案3】:

      如果你想从查询参数中获取数据,那么你应该像下面的代码一样:

      server.route({
          method: 'PUT',
          path: '/lists/{username}/{listname}',
      
          validate: {
              params: {
                  username: Joi.string().min(1).max(15).required(),
                  listname:Joi.string().min(1).max(15).required(),
      
              },
         query: {
                  token:    Joi.number().integer().min(1).max(100).required(),
                  resource_id: Joi.number().integer().min(1).max(100).required(),
               }
             },
          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);
               reply("Hello There..");
          }
      

      您可以使用上面的代码来获取参数/查询数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-18
        • 2023-03-08
        • 1970-01-01
        • 2014-11-30
        • 1970-01-01
        • 1970-01-01
        • 2021-07-20
        • 1970-01-01
        相关资源
        最近更新 更多