Rutik Wankhade
07/28/2022, 6:26 PMJacob Martin
07/28/2022, 6:32 PMprisma.post.update({
where: { id },
data: {
postLikes: {
create: { userId },
},
},
});Jacob Martin
07/28/2022, 6:34 PMJacob Martin
07/28/2022, 6:35 PMJacob Martin
07/28/2022, 6:35 PMcreate for explicit relationship, connect for implicit relationshipJacob Martin
07/28/2022, 6:36 PMRutik Wankhade
07/29/2022, 4:49 AMRutik Wankhade
07/29/2022, 4:49 AMprisma.post.update() invocation:`
{
where: {
? id?: String
},
data: {
likedByUsers: {
create: {
id: 'cl6341sfa0006akiq5qbr'
}
}
}
}
Argument where of type PostWhereUniqueInput needs at least one argument. Available args are listed in green.Nurul
07/29/2022, 6:53 AMid being passed in the where cause of update query is correct and not null or undefinedRutik Wankhade
07/29/2022, 9:56 AMmodel User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String? @unique
password String?
emailVerified DateTime?
image String?
role String? @default("user")
accounts Account[]
sessions Session[]
posts Post[]
likedPosts Post[] @relation("likedPosts")
}
model Post {
id String @id @default(cuid())
created DateTime @default(now())
userName String?
title String?
category String?
author User? @relation(fields: [authorId], references: [id])
authorId String?
likedByUsers User[] @relation("likedPosts")
@@index([authorId])
}Rutik Wankhade
07/29/2022, 9:57 AMRutik Wankhade
07/29/2022, 9:58 AMtry {
const body = { id };
console.log(body)
await fetch('/api/like', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
} catch (error) {
console.error(error);
}Rutik Wankhade
07/29/2022, 10:01 AMexport default async function handle(req, res) {
const session = await getSession({ req });
const { id } = req.body;
const { userId } = session;
const result = await prisma.post.update({
where: {id},
data: {
likedByUsers: {
create: { id:userId},
},
},
});
res.json(result)
}Nurul
07/29/2022, 10:55 AMconsole.log(req.body.id) then are you getting the id in the log?Rutik Wankhade
07/29/2022, 10:59 AMNurul
07/29/2022, 11:05 AMimport { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
log: ['query'],
});
async function main() {
await prisma.user.deleteMany();
await prisma.post.deleteMany();
const userId = 'cl66cp94a00015378llsicsp4';
const postId = 'cl66crerw00129578qo29qenz';
await prisma.user.create({
data: {
id: userId,
email: 'sundarani@prisma.io',
name: 'Sundarani',
posts: {
create: {
id: postId,
title: 'My first post',
},
},
},
});
await prisma.post.update({
where: {
id: postId,
},
data: {
likedByUsers: {
connect: {
id: userId,
},
},
},
});
console.log('Finished Execution');
}
main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
Here’s the output where likedByUsers shows the user who has liked the post:Rutik Wankhade
07/29/2022, 11:33 AMconst {postId}= body
I passed directly as
where{
id:req.body.postId
}
somehow fixed it. been stuck since yesterday. Thank you for your time.Rutik Wankhade
07/29/2022, 11:37 AMfollowers and following but it LIKE example would be a nice additionNurul
07/29/2022, 11:41 AM