hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.54k stars 289 forks source link

mongodb call too ussually #84

Closed IRediTOTO closed 4 years ago

IRediTOTO commented 4 years ago

Describe the bug hi, i got this error from mongodb, i searched for it on google https://jira.mongodb.org/browse/NODE-1868 problem because call mongodb too many time.

the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [srvHost] is not supported

So I think to create a middleware called end.js. Is this the best way to fix this error ? Thank you

import { mongoClient } from "./database";

export default (req, res, next) => {
    mongoClient.close()
    console.log("close connect db")
    next()
};
hoangvvo commented 4 years ago

Are you in the dev environment? I think this is caused by React HMR.

IRediTOTO commented 4 years ago

Are you in the dev environment? I think this is caused by React HMR.

yes, in local, I just checked on server, look like every thing work ok. "React HMR" ? You mean fetch api 2 times on client side ?

hoangvvo commented 4 years ago

Sorry, React HMR is Hot Module Replacement. The server sorta "restarts" every time you make a change, so the mongodb connect function runs multiple times.