There is no assigned value in scope for the shorthand property. You must either declare one or provide an initializer

I'm just starting out with TypeScript.

Encountering the error 'No value exists in scope for the shorthand property 'firstName'. Either declare one or provide an initializer.' while using Prisma with Next.js to create a new user in my database.

The user model in schema.prisma:

model User {
  id            Int      @id @default(autoincrement())
  email         String   @unique
  firstName     String
  middleName    String?
  lastName      String
  dateOfBirth   DateTime
  mobileNumber  String   @unique
  idType        String
  idNumber      String   @unique
  idExpirtyDate DateTime
  idIssueState  DateTime
  streetAddress String
  suburb        String
  postCode      Int
  state         String
  bsb           Int      @unique
  accountNumber Int      @unique
  payIdType     String
  payId         String   @unique
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt
}

The createUser() function looks like this:

export async function createUser(user: FormData) {
  const prisma = new PrismaClient()

  await prisma.user.create({
    data: {
      firstName,
      lastName,
      middleName,
      email,
      dateOfBirth,
      mobileNumber,
      idType,
      idNumber,
      idExpirtyDate,
      idIssueState,
      streetAdddress,
      suburb,
      postCode,
      state,
      bsb,
      accountNumber,
      payIdType,
      payId,
    },
  })
}

This is where I'm getting the FormData, from a component:

  async function handleFormData(userData: FormData) {
    'use server'
    await createUser(userData)
  }

Any assistance would be greatly appreciated.

Answer №1

The {firstName} shorthand is only effective when there exists a variable named firstName within scope, functioning essentially as {firstName: firstName}.

To access the parameters from the FormData, manual extraction is necessary.

data : {
    firstName: user.get('firstName'),
    // etc.
}

If preferred, all values from the FormData can be passed by converting it into an object using Object.fromEntries.

data: Object.fromEntries(user)

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

How come the array's length is not appearing on the browser screen?

Code: initialize: function() { this.todos = [ {id: 100, text: 'Rich'}, {id: 200, text: 'Dave'} ]; }, activeTodos: function() { this.todos = this.todos.length(function() { return this.todos; }); ...

The jQuery UI Sortable functions are being triggered at lightning speed

I am currently working on a project where users can create a seating chart, add rows and tables, and move the tables between different rows. The functionality for adding rows and moving tables already exists in the code. However, I am facing an issue where ...

"Utilize NextJS to generate pages with either getServerSideProps or getStaticProps method

After encountering high TTFb time when using getServerSideProps to fetch internal API data and experiencing slow page performance, I am exploring alternative fetching strategies. My MongoDB data is relatively small (DATABASE SIZE: 33.84KB) and does not cha ...

Leveraging Global SCSS Variables in Next.JS with SASS

In my Next.js Application, I have a global CSS file named main.scss imported in the pages/_app.js file. _app.js import '../global-styles/main.scss' export default function MyApp({ Component, pageProps }) { return <Component {...pageProps} ...

Using destructuring assignment in a while loop is not functional

[a,b] = [b, a+b] is ineffective here as a and b are always set to 0 and 1. However, using a temporary variable to swap the values does work. function fibonacciSequence() { let [a, b, arr] = [0, 1, []] while (a <= 255) { arr.concat(a) [a, ...

Tips for ensuring only one dropdown is opened at a time

Hey there! I've been struggling with getting my dropdowns to open one at a time on my website. I attempted to hide them all by default, but they still insist on opening simultaneously. Any ideas on what I might be overlooking? Thank you for your help! ...

When using Expressjs MVC, encountering difficulties in retrieving data from mongoose in the listAll() function within the router

I'm currently working on implementing MVC-like architecture in Express.js for a very specific scenario. I suspect there may be an issue with promises, but I'm struggling to debug the problem effectively. Here's how the architecture is set u ...

Tips for sorting an array with various data types in TypeScript while explicitly defining the type

I need help with a class that contains two fields, each being an array of different types but sharing the common field id. I am trying to create a method that filters the array and removes an item based on the provided id. enum ItemType { VEGETABLES = &a ...

Notify users with a prompt when a modal or popup is closed on Google Chrome extensions

I have developed a Google Chrome extension for setting timers and receiving alerts. Currently, the alert only goes off when the extension is open, but I want it to fire even when the extension is closed. This extension currently requires the window to be ...

What steps can I take to ensure that the original field does not regain focus once the dialog is closed?

My users prefer not to switch from the keyboard to mouse while filling out an input form. To address this, I am using the onFocus event to display a JQuery UI Dialog. However, when I use the dialog's close(); function, the dialog reopens as the origin ...

Trouble confirming the password field with regular expressions in angular.js

I'm trying to validate my password field with specific special characters requirements. The field must contain at least one number, upper case letter, lower case letter, and an underscore, all of which are mandatory. I have attempted to achieve this u ...

Click on the button to reveal the hidden content within the div, and then smoothly scroll down to view

I have a footer div that is present at the bottom of every page on our site. I've added a button to expand this div, but I'm looking for a way to automatically scroll the page down so that the user can view the expanded content without manually s ...

The Videojs controls are unresponsive to clicks

Having a strange issue with videojs. I've been attempting to load videojs in the same way as outlined in the documentation, using a dynamic video tag. videojs(document.getElementById('myVideo'), { "controls": true, "autoplay": false, "prelo ...

Creating a custom function in JavaScript to interact with the `windows.external` object specifically for use

In my current project, I am facing an issue with JavaScript file compatibility across different browsers. Specifically, I have a JavaScript file that calls an external function (from a separate file) using windows.external, like this: windows.external.se ...

I have developed a Next.JS-Redux application that simultaneously compiles two pages

Upon redirecting with window.location.href, I encountered the following compilations in the terminal: wait - compiling /login/verify (client and server)... wait - compiling /login (client and server)... Instead of going to /login/verify as expected, it ...

JavaScript: Exporting and Utilizing a Function within a Model.js File

Coming from a background in PHP OOP development, I am aware that there are various methods to create classes in JavaScript. I require assistance from a JavaScript developer to resolve this particular issue. Here is the situation: I am building an AWS lamb ...

Unable to delete event listeners from the browser's Document Object Model

Issue at hand involves two methods; one for initializing event listeners and the other for deleting them. Upon deletion, successful messages in the console confirm removal from the component's listener array. However, post-deletion, interactions with ...

Unable to retrieve push token for the device. Please verify the validity of your FCM configuration

Looking for a solution to the issue of obtaining a push token Error: Unable to retrieve push token for device. Ensure that your FCM configuration is correct I have integrated Expo permissions and notifications in my React Native app, but nothing seems to ...

Experimenting with a sample post on a minimalist Node.js application using Mocha and Superagent

I trust you are having a splendid day. Currently, I am focusing on enhancing my TDD skills in Node.js. To practice, I have developed a minimalistic application that handles basic GET and POST requests. The app simply displays a straightforward form to the ...

Is there a discrepancy in the value between data and computed properties in Vue components?

Upon reviewing my code, I have noticed that the value shown in the data of the component does not match the desired value. The code snippet is provided below: View.component('xxx-item',{ props:['item'], template:`xxxx`, computed: ...