I was following the tutorial Building Modern Applications with Next.js and MongoDB | MongoDB but get an internal server error 500 when calling the api to mongoDB. Upon further investigation, I found that the response from the api call was ‘client.isConnected is not a function’.
After doing some searching, I think the most likely reason is that the code in the tutorial is outdated hence it gives an error. I modified this part of the code according to something I saw from my searching and it seems to work.
async function database(req, res, next) {
// original code
// if (!client.isConnected()) await client.connect();
// modified code: to not check for client.isConnected
await client.connect();
req.dbClient = client;
req.db = client.db('MCT');
return next();
}
I removed the condition checking for client.isConnected as I read it may not be needed anymore, but I’m not sure about this so I was wondering if this is the correct approach and if not, what can I do to solve this issue? Thanks in advance.