Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I setup MongoDB database on Heroku with MongoLab?
    primarykey
    data
    text
    <p>I'm using Express.js and MongoLab and I followed the <a href="https://devcenter.heroku.com/articles/nodejs#using-mongodb">Heroku setup to get MongoDB working</a> in production throwing this code in my <strong>app.js</strong>. </p> <pre><code>//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) { }); }); }); </code></pre> <p>and I have the following routes and field for my email form (also in app.js):</p> <pre><code>//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('/') }); }); </code></pre> <p>This renders the new form on the index page and lets me save it locally but not in production because I don't know how to setup my email collection. Can anyone walk me through this? brand new to using MongoDB and Node.js, so could use some help.</p> <p>EDIT:</p> <p>In The MongoLab Database Interface, I made a <code>collection</code> called <code>emails</code>. Is this the right course of action?</p> <p>EDIT 2:</p> <p>Here's defining EmailProvider in app.js along with the file itself.</p> <p><strong>app.js</strong></p> <pre><code> 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); </code></pre> <p><strong>emailprovider.js</strong></p> <pre><code>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 i =0;i&lt; emails.address;i++ ) { email = emails[i]; email.created_at = new Date(); } email_collection.insert(emails, function() { callback(null, emails); }); } }); }; exports.EmailProvider = EmailProvider; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload