skip to Main Content

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’).

2

Answers


  1. 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)
    }
    ....
    ....
    })
    
    
    Login or Signup to reply.
  2. const { MongoClient, ObjectID } = require('mongodb') // It's Mongo not mnogo
    
    MongoClient
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search