Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Mongoose works locally, but not on Heroku

Here's my node.js file I'm running on Heroku. It works perfectly locally, but when I push it to Heroku, the database collection isn't created and no errors are thrown. Ultimately I'm just trying to get it to actually creating the connection while it's running on Heroku. Thanks.

var mongoose = require('mongoose'),
    db_url = process.env.MONGOHQ_URL || "mongodb://john:<mypasswordwashere>@staff.mongohq.com:10053/app3305538",
    db = mongoose.connect(db_url),
    Schema = mongoose.Schema;

//Mongoose DB Test
var MsgSchema = new Schema({
    date: {type: Date, default: Date.now},
    message: String
});
var MsgModel = db.model("messages", MsgSchema);
var Msg = new MsgModel();
Msg.message = "blurgh";
Msg.save();
like image 747
Johnathan Croom Avatar asked Mar 16 '12 01:03

Johnathan Croom


People also ask

Does MongoDB work with Heroku?

ObjectRocket for MongoDB can be attached to a Heroku application via the CLI: A list of all plans available can be found here. $ heroku addons:create ormongo:5-mmap Creating ormongo-infinite-92036...

Is it mandatory to use Mongoose with Node application?

It's not mandatory to use Mongoose over the MongoDB Native API. However, there are some benefits to doing so.


2 Answers

Did you specify node version in heroku?

please refer this. ( I also confused same issue. but resolved.) http://devcenter.heroku.com/articles/nodejs-versions

like image 74
Takahiro Kubo Avatar answered Oct 08 '22 17:10

Takahiro Kubo


Your call to .save() takes a callback function. Try adding a debug message in there and see if it shows any information about why its failing, like this:

Msg.save(function(err){
  if(err){ 
    console.log("Error:", err);
  }else{
    console.log("success");
  }
})

Then you should be able to see the result in the heroku logs.

like image 29
mpobrien Avatar answered Oct 08 '22 16:10

mpobrien