Hullo !
Basically i used env vars in next.config.js but since i wanted to put the website on github on a public repo, i wanted to secure the .env, so i made a .env.local and put the vars there and used them in next.config.js and then in the app. However, it is not working. An instance of them not working is, I am saving the mongoDB client link as a env variable and using that in the dbConnect.js function however it doesnt work and throws an error saying "No URI is available got undefined"
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
env: {
DB_URL: process.env.DB_URL,
GITHUB_ID: process.env.GITHUB_ID,
GITHUB_SECRET: process.env.GITHUB_SECRET,
LINK: process.env.LINK,
},
};
module.exports = nextConfig;
This is the next.config.js
DB_URL:"link",
GITHUB_ID:"id",
GITHUB_SECRET:"secret",
LINK:"link"
This is .env.local
unhandledRejection: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
Error for dbConnect.js function where i use the env var
const connection = {};
async function dbConnect() {
if (connection.isConnected) {
return;
}
const db = await mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
connection.isConnected = db.connections[0].readyState;
console.log(connection.isConnected);
}
export default dbConnect;
The dbConnect.js function