我收到一个错误
缺少参数
user
使用 prisma/postgresql 创建评论时。
以下是错误的屏幕截图,以便更好地理解:
这是我的模型:
model User {
id String @id @default(uuid())
username String
email String
password String
comments Comment[]
questions Question[]
answers Answer[]
reputation Int @default(0)
createdAt DateTime @default(now())
updated DateTime @updatedAt @default(now())
}
model Question {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
userId String
title String
bodyText String
comments Comment[]
answers Answer[]
upvote String[] @default([])
downvote String[] @default([])
acceptedAnswer String @default("")
views Int @default(0)
tags String[] @default([])
createdAt DateTime @default(now())
updated DateTime @updatedAt @default(now())
}
model Answer {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
userId String
question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
questionId String
bodyText String
comments Comment[]
upvote String[] @default([])
downvote String[] @default([])
createdAt DateTime @default(now())
updated DateTime @updatedAt @default(now())
}
model Comment {
id String @id @default(uuid())
question Question? @relation(fields: [postId], references: [id], onDelete: Cascade, map: "question")
answer Answer? @relation(fields: [answerId], references: [id], onDelete: Cascade, map: "answer" )
postId String
answerId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade, map: "user")
userId String
commentText String
createdAt DateTime @default(now())
updated DateTime @updatedAt @default(now())
}
这是我创建评论的代码:
async commentPost(req, res) {
try {
const { commentText } = req.body;
const { id } = req.params;
const cookies = req.cookies["auth_token"];
// verifyToken is id of the user
const verifyToken =
cookies && jwt.verify(cookies, process.env.JWT_SECRET);
if (verifyToken) {
console.log("this is user id while creating comment", verifyToken);
const comment = await prisma.comment.create({
data: {
commentText,
userId: verifyToken,
postId: id,
},
});
res.status(201).send(comment);
} else {
res.status(404).send("User not found");
}
} catch (error) {
console.log("got error in comment controller commentPost", error);
}
}
您已向模型添加了新的必需列,而数据库中仍有一些使用旧模型创建的记录。删除所有旧记录并重试。