mongoose.connect(), first argument should be String, received undefined

前端 未结 21 2889
既然无缘
既然无缘 2021-02-12 15:55

I am trying to set the test database for the testing purpose, but its not working.

I am trying to connect to mongodb using mongoose, but finding problem in connection er

相关标签:
21条回答
  • 2021-02-12 16:44

    What I was doing wrong was I created js file to store the key:

    module.export = {
    MONGOURI : "Your Key"
    }
    

    and from my app.js I was fetching the key with different keyname like

    const {MongoUri} = require('./keys')
    

    after changing MongoUri to MONGOURI , it worked fine.

    0 讨论(0)
  • 2021-02-12 16:46
    const db = process.env.MONGO || 'test'    
        
    mongoose.connect(db, {
          useNewUrlParser: true,
          useUnifiedTopology:true,
          useCreateIndex: true
        }).then(()=>{
          console.log("conected to mongodb");
        }).catch(error => {
          console.log("mongo error",error);
        })
    
    0 讨论(0)
  • 2021-02-12 16:47

    I had the same problem, but then I realized that I saved the .env file as .env.txt which caused the issue. I deleted the file and created another file without .txt at the end and everything worked find.

    I hope this helps.

    Dhiya Aljaradi

    0 讨论(0)
提交回复
热议问题