When TypeScript error "ts(18004)" occurs, it is because of the object properties within all Prisma DB

I am currently in the process of verifying if a user's email already exists. To achieve this, I am utilizing Prisma Client's findUnique method. Below is the code snippet I have implemented:

const userWithEmail = await prisma.user.findUnique({
    where: {
      email,
    },
  });

However, I encountered a TypeScript error:

No value exists in scope for the shorthand property 'email'. Either declare one or provide an initializer.ts(18004)

Here is my schema.prisma file:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
...

And here is my route.ts file:

import { PrismaClient } from "@prisma/client";
import { NextResponse } from "next/server";
import validator from "validator";
...

I am working with Next.js 13 and Prisma. Can someone please help me identify what I might be doing incorrectly?

Answer №1

It seems like the problem lies in this section:

const body = await request.json();

There is no method request.json(). If you had utilized

import { NextApiRequest } from "next";

TypeScript would have alerted you to this mistake. You should be using NextApiRequest

export interface NextApiRequest extends IncomingMessage {
  query: Partial<{
      [key: string]: string | string[];
  }>;
  cookies: Partial<{
      [key: string]: string;
  }>;
  body: any;
  env: Env;
  preview?: boolean;
  previewData?: PreviewData;
}

Although it extends IncomingMessage, it does not contain a json() property. Instead, you should do

const { email, password } = req.body;

Answer №2

Perhaps the "email" variant is not within the same scope in the method.
For example, if you declare the "email" variant before the findUnique call, this issue will be resolved.

const email = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="661203151226031e070b160a034805090b">[email protected]</a>"
const userWithEmail = await prisma.user.findUnique({
    where: {
      email,
    },
  });

If you do not declare the email variant as "email" (for example, name it mailAddress), try using the following code.

const mailAddress = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e296879196a2879a838f928e87cc818d8d8f">[email protected]</a>"
const userWithEmail = await prisma.user.findUnique({
    where: {
      email: mailAddress,
    },
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Develop an extensive Typescript and React shared library

Trying to develop a shared React and Typescript library has been quite challenging. Configuring the project workspace to work on both the library and application simultaneously has proven to be more difficult than anticipated. project ├─ app │ ├ ...

The issue of Next.js redux useSelector causing HTML inconsistency

Currently, I am utilizing Next.js for the development of a React application. In order to manage the state, I have integrated redux along with redux-toolkit. A peculiar error has surfaced in the console with the message: Warning: Did not expect server H ...

Does the routing in Angular 2 get disrupted by parameter breaks in sub-modules?

In my Angular 2 application, I am encountering an issue with routing to my line module. Currently, I have two submodules - login and line. The routing to the login submodule is working well. However, when I attempt to route to the line module with route pa ...

Issue encountered while executing jest tests - unable to read runtime.json file

I've written multiple unit tests, and they all seem to pass except for one specific spec file that is causing the following error: Test suite failed to run The configuration file /Users/dfaizulaev/Documents/projectname/config/runtime.json cannot be r ...

Issues with APIs surfaced following the transition from DataGridPro to DataGridPremium

In my current project, we initially implemented the MUI X DataGrid but later switched to DataGridPro due to business requirements. Recently, we upgraded our plan from Pro to Premium which has caused some unexpected issues in our existing code. I am using " ...

Using `this` within an object declaration

I am encountering an issue with the following code snippet: const myObj = { reply(text: string, options?: Bot.SendMessageOptions) { return bot.sendMessage(msg.chat.id, text, { reply_to_message_id: msg.message_id, ...options }) ...

How to identify generic return type in TypeScript

My goal is to develop a core dialog class that can automatically resolve dialog types and return values based on the input provided. I have made progress in implementing this functionality, but I am facing challenges with handling the return values. Each ...

Developing a Next.js shared library

I am looking to create Next.js components as shared components that can be utilized across multiple projects. 1- To get started, I have developed a basic component named "TextBox" located at src/lib/component/TextBox.js: import { useState } from "react"; ...

Possibility for Automatic Type Inference in Generics

Is there a way to have a method infer the type of function parameter without specifying its generic? Currently it is 'GET' | 'POST', but I only need the literal 'GET' const func = <Params, Method extends "GET" | & ...

Exploring nested JSON objects within an array using ngFor directive

My application uses Angular 6 and Firebase. I am trying to showcase a list of all appointments. Below is my approach: service.ts getRDV() { this.rdvList = this.firebase.list('/rdv'); return this.rdvList; } Model: export class RDV { key: ...

Steps for creating a copy of an Angular component

https://i.stack.imgur.com/4RMsR.png Whenever the user clicks on the Create Copy button, I aim to replicate the content of the DashboardComponent and position the duplicated version below the original one (the DashboardComponent featuring four dark blue sq ...

Developing a search feature using Angular 6 with Observable subscription for the FrontEnd application

I have a unique challenge where I need to implement a full text search in the FrontEnd due to restrictions with the API. When the frontend starts up, it fetches all data entries from the Backend and subscribes them inside a component using an async pipe. T ...

Error: Unable to access $rootScope in the http interceptor response function

I have set up an interceptor to display an ajax spinner while loading. interface IInterceptorScope extends angular.IRootScopeService { loading: number; } export class Interceptor { public static Factory($q: angular.IQService, $ro ...

Getting a URL path in Next.js without relying on the Link component when the basePath is configured

Base Path from the next.js documentation states: For instance, by setting basePath to /docs, /about will automatically transform into /docs/about. export default function HomePage() { return ( <> <Link href="/about"> ...

Encountering an unexpected redirection issue with Google OAuth2 while using Node.js and Passport

Currently, I am in the process of creating a node.js server utilizing passport middleware to authenticate users through Google's oauth2. The authentication flow can be broken down into the following steps: Users are directed to <server>/aut ...

The Importance of Strict Contextual Escaping in ReactJS

When making an API call, we receive a URL in the response. In Angular JS, we can ensure the safety of this URL by using $sce.trustAsResourceUrl. Is there an equivalent function to trustAsResourceUrl in React JS? In Angular, //Assuming 'response&apos ...

Top authentication approach for Ruby on Rails API-only server, paired with ActiveAdmin

I'm currently working on a project that involves developing the frontend in Next.js using the fetch API for requests, and the backend in RoR. The challenge I'm facing is setting up user authentication. After some research, I've come across f ...

What is the best way to retrieve an element that has been altered in its state?

I encountered a scenario where I want an image to have a border when clicked, and if clicked again, the border should be removed. However, the border should also be removed if another image is clicked instead. I believe there are a couple of approaches to ...

Issue with the code: Only arrays and iterable objects are permitted in Angular 7

Trying to display some JSON data, but encountering the following error: Error Message: Error trying to diff 'Leanne Graham'. Only arrays and iterables are allowed Below is the code snippet: The Data {id: 1, name: "Leanne Graham"} app.compone ...

Challenge with concatenating Nextjs Links

I am encountering an issue with the Next/Link component. The problem arises when a user reaches the product details page. On the homepage, there are 3 link components that direct users to domain.com/headphones, domain.com/earphones, or domain.com/speakers. ...