user10158754
user10158754

Reputation:

Why mongodb won't create a new database?

Whenever I try to register a new user on my website or search for something stored in the database the connection can't get through to the database. I am using node.js and MongoDB. This is how I configured the connection to the server in app.js

mongo.connect("mongodb://localhost:27017/Tuskdb", { useNewUrlParser: true });

When I check the mongo console for databases with the command show dbs I don't get my database listed. However, when I run my server using nodemon app.js I get no errors related to the database even though the connection doesn't go through. It just gives me a

This page isn’t working

localhost didn’t send any data.

ERR_EMPTY_RESPONSE

in the browser, but nothing in the console. When I try to create a new database by changing my route to say :

mongo.connect("mongodb://localhost:27017/newdb", { useNewUrlParser: true });

It won't do it. What do you think happened? I am not sure what kind of additional information I need to provide, so let me know in the comments.

Upvotes: 0

Views: 1045

Answers (1)

Abdullah Khan
Abdullah Khan

Reputation: 645

first you have to add error call back function an print it

mongoose.connect(`"mongodb://localhost:27017/Tuskdb"`, function(error) {
    if(error){
        console.log("error in creating connection",error);
    }else{
    console.log(`monogodb connected)
    }
  });   

Upvotes: 1

Related Questions