Unverified Commit 74fb7ba0 authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

[Docs] Update mongodb example (#39658)



## Changelog

- Updated deps
- Migrated to typescript

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 1ebd6a1d
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
if (!process.env.MONGODB_URI) {
throw new Error('Invalid environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI const uri = process.env.MONGODB_URI
const options = {} const options = {}
......
...@@ -6,9 +6,14 @@ ...@@ -6,9 +6,14 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"mongodb": "^4.1.3", "mongodb": "^4.8.1",
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2" "react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.5",
"@types/react": "16.9.17",
"typescript": "4.6.3"
} }
} }
import Head from 'next/head' import Head from 'next/head'
import clientPromise from '../lib/mongodb' import clientPromise from '../lib/mongodb'
import { InferGetServerSidePropsType } from 'next'
export default function Home({ isConnected }) { export async function getServerSideProps(context) {
try {
await clientPromise
// `await clientPromise` will use the default database passed in the MONGODB_URI
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
//
// `const client = await clientPromise`
// `const db = client.db("myDatabase")`
//
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands
return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}
export default function Home({
isConnected,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return ( return (
<div className="container"> <div className="container">
<Head> <Head>
...@@ -221,26 +247,3 @@ export default function Home({ isConnected }) { ...@@ -221,26 +247,3 @@ export default function Home({ isConnected }) {
</div> </div>
) )
} }
export async function getServerSideProps(context) {
try {
await clientPromise
// `await clientPromise` will use the default database passed in the MONGODB_URI
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
//
// `const client = await clientPromise`
// `const db = client.db("myDatabase")`
//
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands
return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
import { MongoClient } from 'mongodb'
declare global {
var _mongoClientPromise: Promise<MongoClient>
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment