📜  为什么 node.js 中的 .env 文件无法连接 mongodb - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:08.789000             🧑  作者: Mango

代码示例2
// Create .env file 
    inside .env 
    MONGO_URI=mongo+src://username:password@cluster0......majority
    //
   // Inside app.js or server.js 
    const dotenv = require('dotenv')
    dotenv.config()

    const mongoose = require('mongoose')

    mongoose.connect(process.env.MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    const db = mongoose.connection
    db.on('error', error => console.error(error))
    db.once('open', MONGO_URI => console.log('Connected to Database'))