I have been trying to deploy my sails app on heroku using mongolab as my mongodb.
I have used the following connections, one for development and one for production:
local_mongo : {
adapter : 'sails-mongo',
host : 'localhost',
port : 27017,
database : "something"
},
heroku_mongolab : {
adapter : "sails-mongo",
url : "mongodb://*****:******[email protected]:63160/something"
}
I have also set my local.js file to use development environment and out that file on gitignore.
Finally on my development.js I do the following
module.exports = {
models: {
connection: 'local_mongo'
}
};
on my production.js
module.exports = {
/***************************************************************************
* Set the default database connection for models in the production *
* environment (see config/connections.js and config/models.js ) *
***************************************************************************/
models: {
connection: 'heroku_mongolab'
}
/***************************************************************************
* Set the port in the production environment to 80 *
***************************************************************************/
//port: 80
/***************************************************************************
* Set the log level in production environment to "silent" *
***************************************************************************/
//log: {
// level: "silent"
//}
};
and on my package.json I changed the start script to node app.js --prod. When running locally everything works as expected, even when I run npm start, but when I deploy on heroku and look at the logs I see that it tries to connect to the localhost mongodb.
Is it a bug or am I doing something wrong?
Yes, I am also experiencing exactly the same problems.
I tried commented out the config/env/development.js
and set the local.js (to connect to localMongo) and set the model.js (to connect to ProductionMongo).
However still have no luck, and the log on heroku still saying Error: failed to connect to [localhost:27017]
Apart from that, this Problem doesn't exist, if I set the development.js to connect other DB than Mongo connection.
So I think this is a bug regarding to the adapter: 'sails-mongo'.
I had the same problem. Worked around it by setting:
config/env/development.js:
connections: {
localMongo: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
...
}
},
models: {
connection: 'localMongo'
}
config/env/production.js
connections: {
productionMongo: {
adapter: 'sails-mongo',
host: 'remotehost1',
port: 27017,
...
}
},
models: {
connection: 'productionMongo'
}
Same issue in here but probably it can be easily fixed in the future!
Same issue. I used @leobudima solution.
Same issue here and it only exists on Heroku, if I start my app locally with a NODE_ENV=production vs. NODE_ENV=development everything works fine... the NODE_ENV i set in my ProcFile and also via heroku tool belt no luck.
I want to do the "right" thing and use connections.js but I think I am going to have to do what @leobudima has done.
For what it's worth, I didn't encounter this issue until I upgraded my dyno to the cedar-14 stack. My project had until now been running on cedar (which is deprecated) using Sails v10.5, but immediately upon upgrading I experienced the same issue with my development connection being used rather than production.
The solution provided by @leobudima served as a good temporary fix.
I'm having the same issue but none of the suggestion above seem to work, so don't know what I'm missing.
Im having the same issue, setting environment variables doesnt seem to do anything. I also tried the solution @leobudima mentioned and thats not working. Still looks to localhost for me no matter what.
I've confirmed that no matter what, it always defaults to whatever connection you have in /config/env/development.
I finally found the solution, in the package json from the sails app you have to change the start script.
"scripts": {
"start": "node app.js --NODE_ENV",
"debug": "node debug app.js"
},
and add the NODE_ENV variable to heroku.
heroku config:set NODE_ENV=prod
Both @leobudima and @agustin3a solutions did not work for me. What causes the default to local?
Apparently having multiple connections with the same adapter can cause the problem: http://stackoverflow.com/questions/22775028/sails-fails-to-load-when-using-remote-mongo-db
I was seeing this issue - and removing other 'sails-mongo' configurations from config/connections.js seemed to fix it for me. There are other SO posts that contend one could put those directly in the config/env/* files as a potential fix as well. I assume in those fixes one would also remove any reference to the adapter in use from config/connections.js.
Thanks for posting, @gkaimakas. I'm a repo bot-- nice to meet you!
It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:
Thanks so much for your help!
I'm having the exact same problem on Bluemix
2016-10-28T02:55:01.63-0200 [App/0] ERR
2016-10-28T02:55:01.63-0200 [App/0] ERR /home/vcap/app/node_modules/mongodb/lib/server.js:236
2016-10-28T02:55:01.63-0200 [App/0] ERR process.nextTick(function() { throw err; })
2016-10-28T02:55:01.63-0200 [App/0] ERR ^
2016-10-28T02:55:01.64-0200 [App/0] ERR Error: connect ECONNREFUSED 127.0.0.1:27017
2016-10-28T02:55:01.64-0200 [App/0] ERR at Object.exports._errnoException (util.js:907:11)
2016-10-28T02:55:01.64-0200 [App/0] ERR at exports._exceptionWithHostPort (util.js:930:20)
2016-10-28T02:55:01.64-0200 [App/0] ERR at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)
2016-10-28T02:55:01.83-0200 [App/0] ERR npm ERR! Linux 3.19.0-33-generic
2016-10-28T02:55:01.83-0200 [App/0] ERR npm ERR! argv "/home/vcap/app/vendor/node/bin/node" "/home/vcap/app/vendor/node/bin/npm" "start"
2016-10-28T02:55:01.84-0200 [App/0] ERR npm ERR! node v4.6.0
2016-10-28T02:55:01.84-0200 [App/0] ERR npm ERR! npm v2.15.9
2016-10-28T02:55:01.84-0200 [App/0] ERR npm ERR! code ELIFECYCLE
2016-10-28T02:55:01.85-0200 [App/0] ERR npm ERR! Exit status 1
2016-10-28T02:55:01.85-0200 [App/0] ERR npm ERR!
2016-10-28T02:55:01.85-0200 [App/0] ERR npm ERR! Failed at the [email protected] start script 'node app.js --prod'.
2016-10-28T02:55:01.85-0200 [App/0] ERR npm ERR! This is most likely a problem with the elinke-aovivo-server package,
2016-10-28T02:55:01.85-0200 [App/0] ERR npm ERR! not with npm itself.
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR! node app.js --prod
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR! npm bugs elinke-aovivo-server
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR! Or if that isn't available, you can get their info via:
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR!
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR! npm owner ls elinke-aovivo-server
2016-10-28T02:55:01.86-0200 [App/0] ERR npm ERR! There is likely additional logging output above.
2016-10-28T02:55:01.89-0200 [App/0] ERR npm ERR! Please include the following file with any support request:
2016-10-28T02:55:01.89-0200 [App/0] ERR npm ERR! /home/vcap/app/npm-debug.log
I am using sails 0.12.10, and sails-mongo 0.12.5, I still can not connect to remote mongo db. And I also tried all possible solutions suggested by people.
I had the same problem, so combined the @leobudima and @agustin3a solutions and works fine for me.
PS: Let just one connection configured in config/connections.js
Most helpful comment
I finally found the solution, in the package json from the sails app you have to change the start script.
and add the NODE_ENV variable to heroku.