1

I am having issue connecting to mongodb atlas, getting the error below

let express = require('express')
let mongodb = require('mongodb')
let app = express()
let db

let connectionString = 'mongodb+srv://olumide:[email protected]/todoApp?retryWrites=true&w=majority'
mongodb.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, client){
    db = client.db()
    app.listen(3000)
})

app.use(express.urlencoded({extended: false}))

app.post('/create-item', function(req,res){
   db.collection('item').insertOne({text: req.body.item}, function(){
    res.send("thanks for submitting the form")
   })

})

Error message

2
  • 1
    The error message means that client is null in client.db - have you checked if err has any content? Commented Jan 1, 2020 at 22:00
  • Please post the error as text, not an image. Commented Jan 2, 2020 at 0:11

3 Answers 3

1

This is because the mongo.connect function is asynchronous. You will need to include the app.post function inside of the mongo.connect callback.

Something kind of like this should work:

let express = require('express')
let mongodb = require('mongodb')
let app = express()

app.use(express.urlencoded({extended: false}))

let connectionString = 'mongodb+srv://olumide:[email protected]/todoApp?retryWrites=true&w=majority'
mongodb.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, client){
    const db = client.db()

    app.post('/create-item', function(req,res){
       db.collection('item').insertOne({text: req.body.item}, function(){
        res.send("thanks for submitting the form")
       })
    })

    app.listen(3000)
})
Sign up to request clarification or add additional context in comments.

Comments

1

I figured out, in newer versions of MongoDB (3 and higher) they have essentially changed the way of connecting node server to the database. To establish a reusable connection (So that we can access the connected database from any other file), I created an async function in my db.js file where the connection is established and then exported it. In the end of the file, I have called the function. The code is as follows:

const {MongoClient} = require('mongodb')


const client = new MongoClient('mongodb+srv://todoAppUser:<password>@cluster0.6lvjr.mongodb.net/myDatabase?retryWrites=true&w=majority')

async function start(){
  await client.connect()
  console.log("Connected")
  module.exports = client.db()
  const app = require('./app')
  app.listen(3000)
}

  start()

and while calling it from another file:

const productCollection = require('./db').collection("product");

This code gives me no error and works perfectly fine. With the help of the above code, one can use this conveniently while following the MVC (Model-View-Controller) framework.

Comments

0

use mongoose connection as a different javascript file and import it to express script file database.js

let mongoose = require('mongoose');
const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVER
const database = 'test';      // REPLACE WITH YOUR DB NAME
class Database {
    constructor() {
      this._connect()
    }
    
  _connect() {
       mongoose.connect(`mongodb://${server}/${database}`,{ useUnifiedTopology: true ,useNewUrlParser: true, useFindAndModify: false})
         .then(() => {
           console.log('Database connection successful')
         })
         .catch(err => {
           console.error('Database connection error')
         })
    }
  }
  
  module.exports = new Database()

strong text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.