From b290a3dc38fedccf71b62c94eefc76af9aff1453 Mon Sep 17 00:00:00 2001 From: "Kim, Harim" Date: Wed, 1 Dec 2021 06:50:50 +0900 Subject: [PATCH] Fix with-mongo example by removing deprecated function (#30675) * Remove deprecated function * Remove useless change * lint-fix Co-authored-by: JJ Kasper --- examples/with-mongodb/lib/mongodb.js | 5 +---- examples/with-mongodb/package.json | 2 +- examples/with-mongodb/pages/index.js | 27 +++++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/with-mongodb/lib/mongodb.js b/examples/with-mongodb/lib/mongodb.js index 2990cffd2cac4..0eef49bf49936 100644 --- a/examples/with-mongodb/lib/mongodb.js +++ b/examples/with-mongodb/lib/mongodb.js @@ -1,10 +1,7 @@ import { MongoClient } from 'mongodb' const uri = process.env.MONGODB_URI -const options = { - useUnifiedTopology: true, - useNewUrlParser: true, -} +const options = {} let client let clientPromise diff --git a/examples/with-mongodb/package.json b/examples/with-mongodb/package.json index fd7f9f0ba2603..f03fe2598d01f 100644 --- a/examples/with-mongodb/package.json +++ b/examples/with-mongodb/package.json @@ -6,7 +6,7 @@ "start": "next start" }, "dependencies": { - "mongodb": "^3.5.9", + "mongodb": "^4.1.3", "next": "latest", "react": "^17.0.2", "react-dom": "^17.0.2" diff --git a/examples/with-mongodb/pages/index.js b/examples/with-mongodb/pages/index.js index 9628afb6523c7..a57dfe93bfb97 100644 --- a/examples/with-mongodb/pages/index.js +++ b/examples/with-mongodb/pages/index.js @@ -223,17 +223,20 @@ export default function Home({ isConnected }) { } export async function getServerSideProps(context) { - const client = await clientPromise - - // client.db() will be the default database passed in the MONGODB_URI - // You can change the database by calling the client.db() function and specifying a database like: - // 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 - - const isConnected = await client.isConnected() - - return { - props: { isConnected }, + try { + // client.db() will be the default database passed in the MONGODB_URI + // You can change the database by calling the client.db() function and specifying a database like: + // 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 + await clientPromise + return { + props: { isConnected: true }, + } + } catch (e) { + console.error(e) + return { + props: { isConnected: false }, + } } }