request.query 不会打印 Fastify 中的查询(querystring)

发布于 2025-01-12 15:12:48 字数 658 浏览 8 评论 0原文

我搜索了很长时间,但它只是不会打印查询,我不知道我应该做什么。我也是 Fastify 的新手。我还将请求发送到 127.0.0.1/?greeting=something

const opts = {
  schema: {
    querystring: {
      type: 'object',
      required: ['greeting'],
      properties: {
        greeting: {type: 'string'},
      },
    },

    response: {
      200: {
        type: 'object',
        properties: {
          status: {type: 'object'}, // i've abosulutely no idea what the type should be
        },
      },
    },
  },

  handler: async (request, reply) => {
    reply.send({
      status: request.query,
    })
  }
}

有人可以帮我解决这个问题吗?

I searched for ages, but it just wouldn't print the query, I've no idea what I should do. I'm also kind of new to Fastify. Also I'm sending the request to 127.0.0.1/?greeting=something.

const opts = {
  schema: {
    querystring: {
      type: 'object',
      required: ['greeting'],
      properties: {
        greeting: {type: 'string'},
      },
    },

    response: {
      200: {
        type: 'object',
        properties: {
          status: {type: 'object'}, // i've abosulutely no idea what the type should be
        },
      },
    },
  },

  handler: async (request, reply) => {
    reply.send({
      status: request.query,
    })
  }
}

Can someone please help me in resolving this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2025-01-19 15:12:48

由于响应的架构,您看不到 request.query 输出。

响应架构过滤掉所有未定义的字段,因此 properties 字段将 status 键列为没有任何属性的对象。

您应该将其更改为:

status: { type: 'object', additionalProperties: true }

或添加 greetings 属性。

您可以在这里阅读: https://github.com/fastify/fast-json -stringify#additionalProperties

You don't see the request.query output because of the response's schema.

The response schema filters out all the fields that are not defined, so the properties field lists the status key as object without any properties.

You should change it to:

status: { type: 'object', additionalProperties: true }

or adding the greetings property.

You can read about it here: https://github.com/fastify/fast-json-stringify#additionalProperties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文