Cheese
01/07/2021, 11:46 PMRyan
01/08/2021, 7:31 AMPrismaClient
instance in a React component? If so, that’s not possible as Prisma can only be used on the backend. So it would only be used in getServerSideProps
or getStaticProps
to fetch data.Cheese
01/08/2021, 10:22 AMRyan
01/08/2021, 10:26 AMCheese
01/08/2021, 10:27 AMCheese
01/08/2021, 11:04 AMCheese
01/08/2021, 11:04 AMexport async function getServerSideProps() {
const allUsers = prisma.profile.findMany()
console.log(allUsers)
return {
props : { allUsers }
}
}
Ryan
01/08/2021, 11:06 AMawait
before prismaCheese
01/08/2021, 11:23 AMCheese
01/08/2021, 11:32 AMexport default function UserPage(allUsers){
return (
<div key={allUsers.id}>
<Head>
<title>{allUsers.username}</title>
</Head>
<h1>{allUsers.username}</h1>
</div>
)
}
Cheese
01/08/2021, 11:33 AMRyan
01/08/2021, 11:33 AMexport default function UserPage({ allUsers }){
return (
<div key={allUsers.id}>
<Head>
<title>{allUsers.username}</title>
</Head>
<h1>{allUsers.username}</h1>
</div>
)
}
Cheese
01/08/2021, 11:35 AMRyan
01/08/2021, 11:36 AMallUsers
is a list so you would need to map
over it. Also make sure you have users in your db. Check the posts example in the above doc for a better idea.Ryan
01/08/2021, 11:38 AMCheese
01/08/2021, 11:38 AMCheese
01/08/2021, 11:49 AMCheese
01/08/2021, 11:50 AMCheese
01/08/2021, 11:50 AMtoString()
to itCheese
01/08/2021, 11:50 AMCheese
01/08/2021, 11:50 AMRyan
01/08/2021, 11:51 AMwhere: { username: 'Cheese' }
to match the username.Cheese
01/08/2021, 11:51 AMCheese
01/08/2021, 11:52 AMCheese
01/09/2021, 3:00 AMexport async function getServerSideProps({ params }) {
const allUsers = await prisma.user.findUnique({
where: {
username: params.username.toString()
}
})
return {
props : { allUsers }
}
}
Cheese
01/09/2021, 3:00 AMCheese
01/09/2021, 3:00 AMCheese
01/10/2021, 8:10 PMCheese
01/10/2021, 8:14 PM