Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert undefined or null to object #79

Open
ollebergkvist opened this issue May 6, 2024 · 0 comments
Open

Cannot convert undefined or null to object #79

ollebergkvist opened this issue May 6, 2024 · 0 comments

Comments

@ollebergkvist
Copy link

ollebergkvist commented May 6, 2024

I get this error:
Cannot convert undefined or null to object

Triggered by making query string a z.object:

querystring: z.object({
    query: z.string()
  }),
import { type FastifyInstance, type FastifyPluginAsync } from 'fastify'

import { ZodTypeProvider } from 'fastify-type-provider-zod'
import z from 'zod'

const scraperRoute: FastifyPluginAsync = async function (
  fastify: FastifyInstance
) {
  fastify.withTypeProvider<ZodTypeProvider>().route({
    method: 'GET',
    url: '/scraper',
    schema: {
      querystring: z.object({
        query: z.string().min(4)
      }),
      response: {
        200: z.object({
          url: z.string().min(2),
          hidden: z.boolean()
        })
      }
    },
    handler: async (request, reply) => {
      // @ts-ignore
      const { query } = request.query

      try {
        const data = await getScraper(query)

        return reply.code(200).send(data)
      } catch (error) {
        fastify.log.error(error)
        return reply.code(500).send()
      }
    }
  })
}

export default scraperRoute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant