I'm working on a little blockChain prototype just to learn it myself and the later work on a bigger project. I've downloaded MongoDB and the "mongod" command Works when I type it in the CMD.
However when trying to start my project where the connection is taking place, I'm only getting the error back, that I can't connect to the Mongo DB.
Here's what I got so far:
//Connect to DB
mongoose.connect("mongodb://localhost:27017", (err) => {
if(err)
return console.log("Cannot connect to DB");
console.log("Database is Connected ", err);
connectionCallback();
});
In the MongoDB Compass, I haven't created anything yet. That's why I don't have any DB in the code snippet after the "mongodb://localhost:27017
.
I've already tried it, while specifying a DB in the connection String.
CodePudding user response:
why don't you try mongodb library in nodejs to create a mongodb client
npm install mongodb
this will install it and to connect through the nodejs you can use the followig lines of code
const uri = "mongodb srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
you can use this line to connect to the db and create a client
const client = new MongoClient(uri);
you can also refer here for more details: visit here
CodePudding user response:
Try changing the URI from mongodb://localhost:27017
to mongodb://127.0.0.1:27017
. This might work.