i have a function in next.js which updates a row using prisma, it is server side. I want…
Jump to turn (7)
- 1i have a function in next.js which updates a row using prisma, it is server side
- 2my function has "use server" inside, rewrite it
- 3does revalidatepath work the same way? or it just reloads the same page
- 4rewrite the function using router.push /admin
- 5router doesn't work on "use server" rewrite the solutions for my case
- 6i have a form that has a link href /admin/${product.id}, how do i retrieve this
- 7what about doing: params:
- User
i have a function in next.js which updates a row using prisma, it is server side. I want to redirect after the insert is completed to the path /admin
- Assistant
To redirect after the update is completed, you can use the
redirectmethod from Next.js. Here's how you can modify your function:codeimport { getSession } from 'next-auth/client' import prisma from '../../lib/prisma' import { redirect } from 'next' export default async function handler(req, res) { const session = await getSession({ req }) if (!session || session.user.role !== 'admin') { return res.status(401).send('Unauthorized') } const id = req.query.id const { name, description } = req.body await prisma.product.update({ where: { id }, data: { name, description } }) return redirect('/admin') }