skip to Main Content
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient

const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'

MongoClient.connect(connectionURL, (error, client) => {
    if (error) {
        return console.log('Unable to connect to database.')
    }

    console.log('Connection successful.')
})  

I’m not getting any errors but still nothing gets printed to the console.

I’m trying to connect to the mongodb server.

2

Answers


    1. Try getting rid of the return in the if statement.

    2. Try using alert() instead of console.log, that’s what I always use for JS.

    I hope this helps!

    Login or Signup to reply.
  1. Try the following code snippet:

    const mongodb = require('mongodb')
    const MongoClient = mongodb.MongoClient
    
    const connectionURL = 'mongodb://127.0.0.1:27017'
    const databaseName = 'task-manager'
    const collectionName = 'tasks'
    
    const client = new MongoClient(connectionURL);
    
    async function main() {
        client.connect()  
        console.log(client)
    }
    
    main()
        .then(console.log)
        .catch(console.error)
        .finally(() => client.close());
    

    Also, refer to the get started guide: https://www.mongodb.com/docs/drivers/node/current/quick-start/connect-to-mongodb/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search