0

i have this following route:


router.route('/create/:categoryIds?').post(admCheck, productController.createProduct)

that can have multiple categoryIds to create the product.

this is the controller interface:

 interface ProductQuery {
            categoryIds?: string[],
  }

this is the rest of the code:


try {

            const { categoryIds } = req.body as ProductQuery;

            const createProductBody = z.object({
                name: z.string(),
                description: z.string(),
                price: z.number(),
                amount: z.number(),
                imageUrl: z.array(z.string()).max(6).optional(),

            })
            const { name, description, price, amount, imageUrl, } = createProductBody.parse(req.body)

            const response = await prisma.product.create({
                data: {
                    name,
                    description,
                    price,
                    amount,
                    images: {
                        create: (imageUrl ?? []).slice(0, 6).map((url) => ({ url })),

                    },
                    categories: {
                        connect: categoryIds?.map(id => ({ id })) || []
                    },
                },
                include: {
                    categories: true,
                    images: true
                }
            })

            res.status(201).send({ response })
        } catch (error) {
            res.status(400).send({
                message: error
            })
        }

the route is working but the categoryId is not working, im using this endpoint:

localhost:4000/api/product/create/[422342ea-08d1-4392-bf3a-52eced550d93]

here the image of the insomnia that may help, i tried many things but its the req.body is not working, but at least the ? is working cause its ignoring the param; i checked and the categoryIds array is returning undefined

enter image description here

Also i will repeat this method some times, for size or stickers, including in the route too.

0 Answers0