From e1bb5d6b721c23234a0127f0c4561c201734f199 Mon Sep 17 00:00:00 2001 From: Ankur Datta <64993082+ankur-arch@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:57:17 +0600 Subject: [PATCH] fix: add `findUniqueOrThrow` example --- .../050-prisma-client-reference.mdx | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/content/200-orm/500-reference/050-prisma-client-reference.mdx b/content/200-orm/500-reference/050-prisma-client-reference.mdx index eadc33a2d1..c261dad180 100644 --- a/content/200-orm/500-reference/050-prisma-client-reference.mdx +++ b/content/200-orm/500-reference/050-prisma-client-reference.mdx @@ -621,13 +621,29 @@ const result = await prisma.user.findUnique({ ### `findUniqueOrThrow()` - +:::note We introduced `findUniqueOrThrow` in v4.0.0. It replaces the [`rejectOnNotFound`](#rejectonnotfound) option. `rejectOnNotFound` is deprecated in v4.0.0. - +::: + +`findUniqueOrThrow` retrieves a single data record similarly to [`findUnique()`](#findunique). However, if no record is found, it throws a `NotFoundError` with a message like: `No found`. -`findUniqueOrThrow` retrieves a single data record in the same way as [`findUnique()`](#findunique). However, if the query does not find a record, it returns `NotFoundError: No User found error`. +Here’s an example of how it works: + +```ts +await prisma.model.findUniqueOrThrow({ + where: { + id: 1, + }, +}); +``` + +:::note + +Replace `model` with the name of your Prisma model, e.g., `prisma.user.findUniqueOrThrow` or `prisma.post.findUniqueOrThrow`. + +::: `findUniqueOrThrow` differs from `findUnique()` as follows: