node.js - How do I setup MongoDB database on Heroku with MongoLab? -


i'm using express.js , mongolab , followed heroku setup mongodb working in production throwing code in app.js.

//mongo on heroku setup var mongo = require('mongodb');  var mongouri = process.env.mongolab_uri ||    process.env.mongohq_url ||    'mongodb://localhost/mydb';   mongo.db.connect(mongouri, function (err, db) {   db.collection('mydocs', function(er, collection) {     collection.insert({'mykey': 'myvalue'}, {safe: true}, function(er,rs) {     });   }); }); 

and have following routes , field email form (also in app.js):

//routes  app.get('/', function(req, res) {     res.render('index', {         title: 'dumbyapp'     }); });  //save new email app.post('/', function(req, res){     emailprovider.save({         address: req.param('address')     }, function( error, docs) {         res.redirect('/')     }); }); 

this renders new form on index page , lets me save locally not in production because don't know how setup email collection. can walk me through this? brand new using mongodb , node.js, use help.

edit:

in mongolab database interface, made collection called emails. right course of action?

edit 2:

here's defining emailprovider in app.js along file itself.

app.js

 var express = require('express')   , routes = require('./routes')   , user = require('./routes/user')   , http = require('http')   , path = require('path')   , emailprovider = require('./emailprovider').emailprovider;  var emailprovider= new emailprovider('localhost', 27017); 

emailprovider.js

var db = require('mongodb').db; var connection = require('mongodb').connection; var server = require('mongodb').server; var bson = require('mongodb').bson; var objectid = require('mongodb').objectid;  emailprovider = function(host, port) {   this.db= new db('localdb', new server(host, port, {safe: false}, {auto_reconnect: true}, {}));   this.db.open(function(){}); };   emailprovider.prototype.getcollection= function(callback) {   this.db.collection('emails', function(error, email_collection) {     if( error ) callback(error);     else callback(null, email_collection);   }); };  //save new email emailprovider.prototype.save = function(emails, callback) {     this.getcollection(function(error, email_collection) {       if( error ) callback(error)       else {         if( typeof(emails.address)=="undefined")           emails = [emails];          for( var =0;i< emails.address;i++ ) {           email = emails[i];           email.created_at = new date();         }          email_collection.insert(emails, function() {           callback(null, emails);         });       }     }); };  exports.emailprovider = emailprovider; 

while connection code in first code box appears correct, emailprovider object isn't using it. instead, in app.js, emailprovider being connected localhost:27017 , database name hardcoded in emailprovider.js 'localdb'.

what want instead use connection information provided in mongolab_uri environment variable in emailprovider, contains host, port, , database name already.

there number of ways go doing this, 1 way move connection code first code box emailprovider constructor, , change constructor takes uri instead of host , port. way, can pass mongolab_uri variable constructor in app.js.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -