I've searched the Internet but couldn't find an answer for my problem. It seems specific to my application, but hopefully not :)
This error occurs both during heroku deployment as well as during dokku deployment.
But to the point.
I have the app.js file which part responsible for db connection looks like this:
/**
* DB connection
*/
var dbUrl;
if(app.get('env') === 'development'){
dbUrl = process.env.MONGOLAB_URI || "mongodb://localhost:27017/test";;
}
var db = require('./database');
db.connect(dbUrl, function(err){
if(err) {
console.log('Unable to connect to MongoDB');
} else {
console.log('MongoDB connected successfully!');
}
});
I'm working on development environment only, so I've removed the production bits.
As you can see, I'm getting dbUrl from heroku environment variables, then I'm requiring db index file which looks like this:
var MongoClient = require('mongodb').MongoClient;
var state = {
db: null
};
exports.connect = function(url, done) {
if(state.db) return done();
console.log("trying to connect to mongodb");
console.log(url);
MongoClient.connect(url, function(err, db){
console.log("mongodb callback");
if(err) return done(err);
state.db = db;
done();
});
};
And than I'm invoking the db.connect method in the app.js file (first code snippet) from the database index file (just above).
And I'm getting this error:
Starting process with command NODE_ENV=development node
server/bin/www
mongodb://user:password#ds011111.mongolab.com:33333/db_name
trying to connect to mongodb <<---- this is console.log from my db.connect method
/app/server/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
State changed from starting to crashed
The error occurs only during heroku or dokku deployment.
My db connection is 100% correct (all the environment variables are ok as well so please don't ask about it), checked it in shell, as well as connecting to it from my localhost within the app.
Basically it works on my localhost, I've deployed it on vps with ubuntu, end it works as well. The problems occurs only when I'm trying to use automation tools.
Any help greatly appreciated
Thanks!
Related
I've added mLab to my Heroku app, I also use mongoose. I tried use connection string from localhost, and it was working(almost). In my server file I use:
var db = mongoose.connection;
if (process.env.MONGODB_URI) {
mongoose.connect('mongodb://heroku_fb82r7lw:bbgj8uliam1psdda88fleu55li#ds161580.mlab.com:61580/heroku_fb82r7lw');
// mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect('mongodb://heroku_fb82r7lw:bbgj8uliam1psdda88fleu55li#ds161580.mlab.com:61580/heroku_fb82r7lw');
// mongoose.connect('mongodb://localhost/fitMe')
}
If I open the app from localhost, it saves things to the db, and can get it back, although not everything, but on heroku it doesn't work at all. I use react with server. I think that something wrong with the routs.. so here is the link to server file :
https://github.com/HelenaVolskaia/Motivation/blob/master/server/app.js
You can set an env variable locally and only use this:
// Connect Mongo
mongoose.Promise = global.Promise; // mongoose promises deprecated, use node - mongoosejs.com/docs/promises
mongoose.connect(config.db.MONGODB_URI);
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
But regardless, add the on connection error handler and see what the error is, so you can dig deeper into why it's not connecting.
I am using node-run-cmd package to start the mongodb server in my app.js file. I know this works because I can see the collections on Robomongo when my script is running as well as the mongod.exe in my list of running processes. The problem is trying to connect to the db called testDB. Below is the commented code.
//start mongodb server
//this works
var nrc = require('node-run-cmd');
var cmd = '..\\mongodb\\mongod --dbpath '+__dirname+'\\db';
nrc.run(cmd);
//setup
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var app = express();
app.use(express.static('public'));
app.set('view engine', 'ejs');
//connect to mongo
//this fails to connect to db testDB
var url = 'mongodb://localhost:27017/testDB';
MongoClient.connect(url, function(err, db) {
if(!err) {
console.log("connection successful");
}
else{
console.log(err.message)
}
});
Here is the err.message
failed to connect to server [localhost:27017] on first connect
Any idea what I am doing wrong here. My assumption is that the db connection is trying before the server has fully started but I am not completely sure.
EDIT:
so that's what it was, timing issue. I tried the following and it connected to the DB. Is there a graceful way of doing this other than what I have here?
function connect(){
var url = 'mongodb://localhost:27017/testDB';
MongoClient.connect(url, function(err, db) {
if (!err) {
console.log("connection successful");
}
else {
console.log(err.message)
}
});
}
setTimeout(connect, 10000);
You should use the callback in the node_run_cmd package (https://www.npmjs.com/package/node-run-cmd#callback-style).
Place your connect function inside the callback.
You will probably also want to only start express here as well.
I'm trying to switch to mongoose from using just the native mongodb and having some trouble.
Connecting to the db as I did previously works fine:
var db;
var passDb = function(req, res, next) {
if (!db) {
mongo.connect(process.env.MONGOLAB_URI, function(err, database) {
if (err) throw err;
db = database;
req.db = db;
next();
});
} else {
req.db = db;
next();
}
}
But the way I'm trying to do it now throws an error:
var mongoose = require('mongoose');
var Poll = require('./app/models/poll');
mongoose.connect(process.env.MONGOLAB_URI);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
The error:
connection error: { [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
]
name: 'MongoError',
message: 'getaddrinfo ENOTFOUND undefined undefined:27017' }
Any ideas what is causing this?
edit: it works when I use the database uri directly, but not when I use the environment variable. I have checked and triple checked that everything is typed correctly, it refuses to use the environment variable and I have to put it directly in the code :S
Ok, I figured out my mistake, I was connecting to the database before calling require('dotenv').load();. Mystery solved!
mongoose.connect(process.env.MONGOLAB_URI); now works!
adding directConnection=true to connection string worked for me
mongoose.connect("MONGO_URI=mongodb://localhost:27017/test-db?directConnection=true
");
i think you forget to define the env variable
or maybe you forget to call config method of dotenv packages on server or starter file
config it
const dotenv = require("dotenv")
dotenv.config()
on your watched file or starter...
then use process.env.VARIABLE
or for testing you mongo connection use simple string url
like that :
mongodb://localhost:27017/database_name
Tracing the documentation here and failing to load bson apparently. After running npm start I receive:
Snippet:
var mongo = require("mongodb").MongoClient;
//connect to db server
mongo.connect("mongodb://localhost:28017/myDb", function(err, db){
if(!err) {
console.log("Connected to Database")
}
else{
console.log("failed to connect");
}
});
I have tried updating/reinstalling the driver modules as well. Totally new to the framework & db and this type of error feels so trivial that it is discouraging that I am unable to figure it out. Help!
The default port for mongodb is 27017 (and then 28017 is for a web status page).
http://docs.mongodb.org/manual/reference/default-mongodb-port/
Try this connect string:
"mongodb://localhost:27017/myDb"
On my local host, I have the following Node code to setup a mongoDB database name "dbname":
users.js:
var MongoClient = require("mongodb").MongoClient,
Connection = require("mongodb").Connection,
Server = require("mongodb").Server;
Users = function(host, port) {
var mongoClient = new MongoClient(new Server(host, port));
mongoClient.open(function (){});
this.db = mongoClient.db("dbname");
};
Users.prototype.getCollection = function (callback) {
this.db.collection("users", function (error, users) {
if (error) callback(error);
else callback(null, users);
});
};
Users.prototype.findAll = function (callback) {
this.getCollection(function (error, users) {
if (error) {
callback(error);
} else {
users.find().toArray(function (error, results) {
if (error) {
callback(error);
} else {
callback(null,results);
}
});
}
});
}
// Bunch of other prototype functions...
exports.Users = Users;
I like to put the above database functionality in one file, and then in my main server file require that file as follows:
server.js:
var Users = require("./users").Users;
var users = new Users("localhost", 27017);
users.findAll(function (err, user) {
// Do something
});
To have this working on localhost is pretty easy. In the command line, I just type the following:
$ mongod # to launch the database server
$ node server.js # to launch the web server
and it works fine. However, now I'm trying to push the whole thing onto Heroku with the mongolab addon
heroku addons:add mongolab
but the database is not running and I have no idea how to make it run. This tutorial explains how to setup mongodb with the mongolab URI, but that's not how my code works, I use a host and a port and I create a new server based on that. How should I change my code for it to work on the heroku app? I want to keep the database code in a separate file, with the prototype functions.
Follow the example here at the "MongoClient.connect" section.
Essentially, you will need to change this part of the code:
Users = function(host, port) {
var mongoClient = new MongoClient(new Server(host, port));
mongoClient.open(function (){});
this.db = mongoClient.db("dbname");
};
To use mongoClient.connect() instead of new MongoClient:
Users = function(url) {
MongoClient.connect(url, function(err, db) {
// Find better way to set this since this callback is asynchronous.
this.db = db;
});
};
If you are using node, I recommend using a library such as mongoose npm install mongoose to handle mongodb interactions. Look at my answer here for how to structure your schemas.
Helped by Xinzz's answer, here's the modified code, so that the mongodb database is initialized with a URI instead of host + port. That's how Heroku initializes the mongodb database, and that's why it wasn't working.
var mongodb = require("mongodb");
var MONGODB_URI = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || "mongodb://localhost", // Make sure to replace that URI with the one provided by MongoLab
db,
users;
mongodb.MongoClient.connect(MONGODB_URI, function (err, database) {
if (err) throw err;
db = database;
users = db.collection("users");
accounts = db.collection("accounts");
var server = app.listen(process.env.PORT || 3000);
console.log("Express server started on port %s", server.address().port);
});
The key here is to declare the variables db and users upfront, assign them a value in the asynchronous callback of the connect function of MongoClient and also start the app (app.listen(...)) in the same callback. Then later in the code I can do the following:
users.find().toArray(function (err, results) {
// Do something
});
I also gave up on all these prototype functions, since they did not really add much.