英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论