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

Here is my node.js file I am running on Heroku. It works fine locally, but when I click it on Heroku, the database collection is not created and there are no errors. Ultimately, I'm just trying to get him to actually create a connection while it works 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(); 
+3
source share
3 answers

Did you specify the version of node in heroku?

please refer to this. (I also confused the same problem, but solved it.) Http://devcenter.heroku.com/articles/nodejs-versions

+6
source

Your .save() call acts as a callback. Try adding a debug message there and see if it shows any information about the reasons for the failure, for example:

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

You can then see the result in heroku magazines.

+2
source

Another possible source of confusion is to search for MongoLab and MongoHQ sources, then the default connection string contained in the Heroku tutorial will break your DB Session:

 var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/mydb'; 

ie: If you use MongoHQ only with your installed ones, you should throw away process.env.MONGOLAB_URI

0
source

Source: https://habr.com/ru/post/1443167/


All Articles