I'm receiving this error while deploying it on Vercel / Netlify. Receiving the same error for both Vercel and Netlify while deployment. Any steps to resolve this issue would be helpful.
Database code:
import mongoose from 'mongoose';
const connection = {};
async function connect() {
if (connection.isConnected) {
console.log('already connected');
return;
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState;
if (connection.isConnected === 1) {
console.log('use previous connection');
return;
}
await mongoose.disconnect();
}
const db = await mongoose.connect(process.env.MONGODB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
});
console.log('new connection');
connection.isConnected = db.connections[0].readyState;
}
async function disconnect() {
if (connection.isConnected) {
if (process.env.NODE_ENV === 'production') {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log('not disconnected');
}
}
}
function convertDocToObj(doc) {
doc._id = doc._id.toString();
doc.createdAt = doc.createdAt.toString();
doc.updatedAt = doc.updatedAt.toString();
return doc;
}
const db = { connect, disconnect, convertDocToObj };
export default db;
Error Screenshot
Error:
(
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongooseError: The
uriparameter to
openUri()must be a string, got \"undefined\". Make sure the first parameter to
mongoose.connect()or
mongoose.createConnection()is a string.","trace":["Runtime.UnhandledPromiseRejection: MongooseError: The
uriparameter to
openUri()must be a string, got \"undefined\". Make sure the first parameter to
mongoose.connect()or
mongoose.createConnection()is a string."," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
)
1)Checkout your connection string from mongoDB.In your .env
file and compare it with the data from the atlas
your user is correct ? your password is correct ? your cluster name is the same? your database name is the same?
1a)Make sure that your .env
is in the root folder!
2)Checkout Network Access IP in mongoDB Atlas. Is it active? If not, establish the new one.
3)If you are using VPN turn it off.
4)And lastly => https://vercel.com/docs/concepts/projects/environment-variables
I hope it helps you! Good luck ;-)