缩写属性的范围中不存在数值。请声明一个或提供一个初始化程序。

huangapple go评论85阅读模式
英文:

No value exists in scope for the shorthand property. Either declare one or provide an initializer

问题

我对TypeScript还不熟悉。

在使用Prisma与Next.js时,当我尝试在数据库中创建新用户时,出现了错误在作用域中不存在shorthand属性'firstName'的值。请声明一个或提供一个初始化程序。

schema.prisma中的User模型如下所示:

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
}

createUser()函数如下:

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,
    },
  })
}

下面是获取FormData的位置,它来自一个组件:

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

任何帮助都将不胜感激。

英文:

I am new to TypeScript.

I am getting the error No value exists in scope for the shorthand property 'firstName'. Either declare one or provide an initializer. when using Prisma with Next.js. I am trying to create a new user in my DB.

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
}

createUser() function is as below.

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,
    },
  })
}

Below is the where I am getting the FormData. It is from a component.

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

Any help is appreciated.

答案1

得分: 0

{firstName} 简写只在作用域内有一个名为 firstName 的变量时才有效,因为它实际上等同于 {firstName: firstName}

你需要手动从 FormData 中提取参数。

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

或者,如果你想将 FormData 中的所有值都传递,你可以使用 Object.fromEntries 将其转换为对象。

data: Object.fromEntries(user)
英文:

The {firstName} shorthand only works if there's a variable with the name firstName in scope, as it is effectively the same as {firstName: firstName}.

You need to manually extract the parameters from the FormData.

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

Alternatively, if you want to just pass all the values from the FormData, you can convert it to an object with Object.fromEntries.

data: Object.fromEntries(user)

huangapple
  • 本文由 发表于 2023年5月26日 07:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定