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.
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)