未验证 提交 bad14484 编写于 作者: L Laura Beatris 提交者: GitHub

Style improvements on MongoDB example (#20515)

I've looked at the example code and saw some consistent issues related to code style. The changes applied to this PR fixes the following points:

- Differences of line breaks styles between multiple files
- Differences of if statements styles
- Unnecessary comment
- A typo on a JSDocs

---

There were line breaks between statements on `pages/index.js`
````
export async function getServerSideProps(context) {
  const { client } = await connectToDatabase()

  const isConnected = await client.isConnected() 

  return {
    props: { isConnected },
  }
}
```` 

And this wasn't being applied to the MongoDB utility:

````
export async function connectToDatabase() {
  if (cached.conn) return cached.conn
  if (!cached.promise) {
    const conn = {}
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
{...}
````

And also, as shown in the snippet above, there are different styles of if statements being used. 

With that being said, the reason I made this PR is because I think that this kind of inconsistent arises questions when a contributor looks to the codebase, even if this is a simple example. 
上级 27bb24fe
......@@ -225,7 +225,7 @@ export default function Home({ isConnected }) {
export async function getServerSideProps(context) {
const { client } = await connectToDatabase()
const isConnected = await client.isConnected() // Returns true or false
const isConnected = await client.isConnected()
return {
props: { isConnected },
......
......@@ -16,30 +16,42 @@ if (!MONGODB_DB) {
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentiatlly
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongo
if (!cached) cached = global.mongo = {}
if (!cached) {
cached = global.mongo = {}
}
export async function connectToDatabase() {
if (cached.conn) return cached.conn
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const conn = {}
const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
}
cached.promise = MongoClient.connect(MONGODB_URI, opts)
.then((client) => {
conn.client = client
return client.db(MONGODB_DB)
})
.then((db) => {
conn.db = db
cached.conn = conn
})
}
await cached.promise
return cached.conn
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册