So after trying a bunch of things, I have gotten a connection to my MongoLab server using Heroku. The only issue is before I actually go to my site, it gives me a Process exited with status 1
(full log below). The app runs perfectly fine on my local server. Below is the app.js code. The app itself is at brads-testing.herokuapp.com.
app.js:
//requires and start up app
var express = require('express');
var mongoose = require('mongoose')
, dbURI = 'localhost/test';
var app = express();
//configures app for production, connects to mongoLab databse rather than localhost
app.configure('production', function () {
//app.use(express.logger());
console.log("production!");
dbURI = 'mongodb://USERNAME:PASSWORD@ds037387.mongolab.com:37387/test';
});
//configures app for general stuff needed
app.configure(function () {
//app.use(express.logger());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/static'));
});
//tries to connect to database. If it does, connected set to true. If not, connected stays false
var connected = false;
mongoose.connect(dbURI);
mongoose.connection.on('open', function () {
console.log("connected!");
connected = true;
//creates new schema for object post and saves it to the database
var postSchema = new mongoose.Schema({
body: String
});
var Post = mongoose.model('Post', postSchema);
app.set('views', __dirname + '/views');
app.set('view engine','jade');
app.get('/', function(request, response) {
response.render('index');
});
app.post('/result', function(request, response) {
if (connected) {
var post = new Post({body: request.body.text});
console.log(post);
post.save(function (err) {
if (err) {
console.log("error!");
} else {
console.log("saved!");
}
});
Post.find(function (err, posts) {
if (!err) {
console.log("found!");
console.log(posts);
response.render('result', {posts: posts});
} else {
connected = false;
var error = "The server had an issue sending/executing the query..."
response.render('result_error', {error: error});
}
});
} else {
var error = "The server didn't end up connecting..."
response.render('result_error', {error: error});
}
});
app.get('/result', function (request, response) {
if (connected) {
Post.find(function (err, posts) {
if (!err) {
console.log("found!");
console.log(posts);
response.render('result', {posts: posts});
} else {
connected = false;
var error = "The server had an issue sending/executing the query..."
response.render('result_error', {error: error});
}
});
} else {
var errorText = "The server didn't end up connecting..."
response.render('result_error', {error: errorText});
}
});
app.listen(process.env.PORT || 5000);
});
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
terminal logs:
2012-08-22T19:19:26+00:00 heroku[web.1]: Starting process with command `node app.js`
2012-08-22T19:19:26+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2012-08-22T19:19:28+00:00 app[web.1]: production!
2012-08-22T19:19:28+00:00 app[web.1]: connected!
2012-08-22T19:19:28+00:00 heroku[web.1]: State changed from starting to up
2012-08-22T19:19:29+00:00 heroku[web.1]: Process exited with status 1
The sad thing is, the connected!
message and the Process exited with status 1
are kind of interchangeable. Sometimes one comes before the other, and sometimes it is the other way around. Hopefully someone can help.
It would be nice to get some more log information. I lieu of that, here are a couple things to try in your own environment:
Some drivers are known not to work with all versions of MongoDB. MongoLab is running 2.0.x, which is the latest stable codeline as of this moment. But as we are on the cusp of the 2.2.x GA release there are some drivers, and specifically some driver versions, that require the latest development release of 2.2.x. I'm not sure if Mongoose is one of them, but you should try running the same version locally that your MongoLab instance is running.
MongoLab databases run in auth mode and users are given privileges to their own databases only (for obvious security reasons). This means that some commands (e.g. list all databases) will fail. So try running your local database in auth mode and authenticating as a non-admin user.