node.js - Cannot connect heroku to mongolabs database -
i new dev , have no idea doing in regards deploying app. locally, can run perfectly. have setup heroku account , mongolabs account. app.js file starts app, , have set in heroku , when heroku logs shows attemps start crashes when cant find mongodb. have code wrong, lost , don't know find answer. have been looking hours. username , password swapped below use correct credentials when trying send 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 creates schema , connects follows. have commented out old code works locally, including see had connected , why assume supposed put 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 me write correct code, or point me in right direction documentation explains how set file can use mongolabs heroku appreciative.
i able solve it. needed use:
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);
then mongoose connection needed:
mongoose.connect('mongodb://heroku_somecodeherebutnotthisonec@ds048704.mongolab.com:48704/heroku_3rjmclqw');
Comments
Post a Comment