Search code examples
javascriptnode.jsherokumongoosedeployment

I deployed my app on heroku and I am getting this error on running heroku logs tail "Error connecting to the database"


I am getting the error because the url in mongoose.connect function is incorrect. What should I put in as the url to connect to the database.

var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost/Blog");

var database = mongoose.connection;

database.on(
  "error",
  console.error.bind(console, "Error connecting to the database")
);

database.once("open", () =>
  console.log("Successfully connected to the database")
);

module.exports = database;

Solution

  • Is a Mongo Database service running on that Heroku machine so that you can access it via localhost? If not, you should install it, otherwise you cannot connect to an non-existent service. Then, check on what port it is running, it is usually localhost:27017.