简体   繁体   中英

Node , TypeError: Cannot read properties of undefined (reading 'connect'). error during mongoClient connection

在此处输入图像描述

How to resolve this error??? during mongodb connection?

const mongoClient=require('mongodb').mongoClient  
const state={
db:null
}
module.exports.connect=function(done){
const url='mongodb://localhost:27017'
const dbname='shopping'

mongoClient.connect(url,(err,data)=>{
    if(err) return done(err)
    state.db=data.db(dbname)

    done()
})

}

module.exports.get=function(){
return state.db
}

It's not mongoClient , replace it with MongoClient . The reason for the error is that it's case sensitive:

const MongoClient=require('mongodb').MongoClient

Also replace MongoClient.connect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM