Cannot connect heroku to mongolabs database

时间:2015-10-31 00:09:19

标签: node.js mongodb heroku

I am a new dev and I have no idea what I am doing in regards to deploying my app. Locally, I can run it perfectly. I have setup a heroku account and a mongolabs account. My app.js file starts my app, and I have that set properly in heroku and when I do the heroku logs it shows that is attemps to start it but crashes when it cant find mongodb. I could have my code completely wrong, I am lost and don't know where to find the answer. I have been looking for hours. The username and password is swapped below but I use the correct credentials when trying to send the file. My app.js file is: var express = require('express'); var app = express(); var mongodb = require('mongodb'); var reddit = require('./routes/reddit'); var enter = require('./routes/enter'); var user = require('./routes/user'); var update = require('./routes/update'); var message = require('./routes/message'); var register = require('./routes/register'); var login = require('./routes/login'); var rate = require('./routes/rate'); var credentials = require ('./routes/request-credentials'); var uri = 'mongodb://removed:removed@ds045734.mongolab.com:45734/bubsta'; mongodb.MongoClient.connect(uri, function(err, db){ if(err) throw err; }); app.use('/', reddit); app.use('/user', user); app.use('/enter', enter); app.use('/update', update); app.use('/login', login); app.use('/message', message); app.use('/register', register); app.use('/rate', rate); app.use('/credentials', credentials); app.use(express.static('public/dist')); app.use(express.static('public/js')); app.use(express.static('public/dist/images')); app.use(express.static('bower_components')); app.listen(1337); console.log('Started on server 1337'); the file that creates the schema and connects is as follows. I have commented out the old code that works locally, but am including it just so you see that I had previously connected and why I assume I am supposed to put my new code here. var mongoose = require('mongoose'); var Schema = mongoose.Schema; // mongoose.connect('mongodb://localhost:27017/Users/mike/project-two/users'); mongoose.connect('mongodb://removed:removed@ds045734.mongolab.com:45734/bubsta'); var User = new Schema({ name: { type: String, index: { unique: true }}, password: String, email: String, positive: Number, negative: Number, points: Number, title: String, sessions: Number, roomFull: Boolean }); module.exports = mongoose.model('user', User); if someone could help me write the correct code, or even point me in the right direction with documentation that explains how to set up a file so that I can use mongolabs with heroku I would be very appreciative.

1 个答案:

答案 0 :(得分:0)

我能够解决它。我需要使用:

var mongoUri = process.env.MONGOLAB_URI

mongo.connect(mongoUri, function(err, db){
    if (err){
        console.log(err);
    }else{
        console.log('success');
    };
});

app.listen(process.env.PORT || 1337);

然后我需要我的猫鼬连接:

mongoose.connect('mongodb://heroku_somecodeherebutnotthisonec@ds048704.mongolab.com:48704/heroku_3rjmclqw');