Having an issue with the PrismaClient not being al...
# prisma-client
c
Having an issue with the PrismaClient not being allowed on NextJS...
r
Hey @Cheese 👋 Are you using the
PrismaClient
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.
c
Ah okay thanks, could I have an example @Ryan?
r
@Cheese here’s an example from the docs https://www.prisma.io/nextjs
c
@Ryan Thanks a bunch I missed that.
🙌 1
Now I'm getting this error
Code
Copy code
export async function getServerSideProps() {
const allUsers = prisma.profile.findMany()
console.log(allUsers)

return {
  props : { allUsers }
}
}
r
You’re missing an
await
before prisma
c
Ah thanks
Copy code
export default function UserPage(allUsers){
  return (
    <div key={allUsers.id}>
     <Head>
     <title>{allUsers.username}</title>
     </Head>
     <h1>{allUsers.username}</h1>
     </div>
)
}
Not getting any data displayed.
r
I think it should be:
Copy code
export default function UserPage({ allUsers }){
  return (
    <div key={allUsers.id}>
     <Head>
     <title>{allUsers.username}</title>
     </Head>
     <h1>{allUsers.username}</h1>
     </div>
)
}
c
Still doesn't display any data.
r
allUsers
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.
You would need to add it as per this example
c
Ah I only want to grab one user and display it like this localhost:3000/Username
r
In that case you would need to use findFirst and get the
username
from Next.js via the params.
c
One more question, I'm getting this error?
Oh nevermind
I just appended
toString()
to it
It works!
Thanks for the help
r
It should be
where: { username: 'Cheese' }
to match the username.
c
I got it working though.
It should match the params of the slug
Copy code
export async function getServerSideProps({ params }) {
const allUsers = await prisma.user.findUnique({
  where: { 
    username: params.username.toString()
  }
})

return {
  props : { allUsers }
}
}
Not sure what's happening here, the name type is clearly in the schema
Now I'm getting this after setting name to unique
Nevermind I fixed it 🙂