mahdi monfared
mahdi monfared

Reputation: 41

connection problem mongoDB with MongoClient

I have some problem about connection to my mongoDB with MongoClient.

const { MnogoClient , ObjectID} = require('mongodb')
const id = new ObjectID()
console.log(id)

const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'
MnogoClient.connect(connectionURL , {useNewUrlParser: true}, (error, client)=>{
if(error){
    return console.log(error)
}

const db = client.db(databaseName)
//insertOne is asyncronuse opration and beacuse we use callback function 

//for the collection we can assign my own property object id
db.collection('users').insertOne({
    _id: id,
    name: 'Mahdi',
    age: 24
} , (error , result)=>{
    if(error){
        return console.log('Unable to insert the document')
    }
    console.log(result.ops)
    
})})

i get TypeError: Cannot read properties of undefined (reading 'connect').

Upvotes: 2

Views: 93

Answers (3)

Rashmiranjan Rout
Rashmiranjan Rout

Reputation: 1

Change the mongodb version to "mongodb": "3.1.10", It will work for sure.

Upvotes: 0

Gaurav Shokhanda
Gaurav Shokhanda

Reputation: 1

const { MongoClient, ObjectID } = require('mongodb') // It's Mongo not mnogo

MongoClient

Upvotes: 0

Rohit Rana
Rohit Rana

Reputation: 116

i think you have a typos error I can see there MnogoClient.connect

MongoClient.connect(connectionURL , {useNewUrlParser: true}, (error, client)=>{
if(error){
    return console.log(error)
}
....
....
})

Upvotes: 2

Related Questions