Created
January 8, 2022 09:13
-
-
Save skolhustick/946d5474821729f2c4d254772c1fb8e5 to your computer and use it in GitHub Desktop.
next-js-prisma-orm-mongodb-pages:index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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