Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save skolhustick/946d5474821729f2c4d254772c1fb8e5 to your computer and use it in GitHub Desktop.
Save skolhustick/946d5474821729f2c4d254772c1fb8e5 to your computer and use it in GitHub Desktop.
next-js-prisma-orm-mongodb-pages:index.js
import React from 'react'
import { getAllUsers } from '../prisma/user'
const Homepage = ({ users }) => {
return (
<div>
{users.map(user => (
<div key={user.id}>{user.name}</div>
))}
</div>
)
}
export const getServerSideProps = async ({ req }) => {
const users = await getAllUsers()
// Convert the updatedAt and createdAt in each user to string
// Otherwise, Next.js will throw an error
// Not required if you are not using the date fields
const updatedUsers = users.map(user => ({
...user,
updatedAt: user.updatedAt.toString(),
createdAt: user.createdAt.toString()
}))
return { props: { users: updatedUsers } }
}
export default Homepage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment