添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
捣蛋的长颈鹿  ·  python 3.x - ...·  2 年前    · 
眉毛粗的电梯  ·  刘佳佳·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to connect to my MongoDB using Mongoose and it gives me the following error.

const { mongoose } = require('mongoose'); const db = 'dburl.com/db' mongoose.connect(db, { useNewUrlParser: true }) .then(() => console.log('MongoDB Connected')) .catch((err) => console.log(err));

I get this Error

mongoose.connect(db, { useNewUrlParser: true })
TypeError: Cannot read property 'connect' of undefined

No need to destructure the mongoose in 1st line. Replace your 1st line of code with the below code. It should be work.

const mongoose = require('mongoose');
  • Change { mongoose } with mongoose
  • Remove useNewUrlParser option. New version of Mongoose does not accept it as option and it will throw an error.
  • const mongoose = require('mongoose');
    const db = 'dburl.com/db'
    mongoose.connect(db)
        .then(() => console.log('MongoDB Connected'))
        .catch((err) => console.log(err));
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.