如何使用MongoLab在Heroku上设置MongoDB数据库? [英] How do I setup MongoDB database on Heroku with MongoLab?

查看:178
本文介绍了如何使用MongoLab在Heroku上设置MongoDB数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Express.js和MongoLab,我遵循 Heroku设置来使MongoDB工作在我的 app.js 中抛出此代码。

I'm using Express.js and MongoLab and I followed the Heroku setup to get MongoDB working in production throwing this code in my 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) {
    });
  });
});

我有以下路由和字段为我的电子邮件表单(也在app.js):

and I have the following routes and field for my 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('/')
    });
});

这会在索引页面上呈现新表单,让我在本地保存,但不在生产中,因为我不知道如何设置我的电子邮件收藏。任何人都可以走过我吗?全新使用MongoDB和Node.js,所以可以使用一些帮助。

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.

编辑:

MongoLab数据库接口,我做了一个收集调用电子邮件。这是正确的行动方案吗?

In The MongoLab Database Interface, I made a collection called emails. Is this the right course of action?

编辑2:

这里定义了app.js中的EmailProvider以及文件本身。

Here's defining EmailProvider in app.js along with the 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 i =0;i< emails.address;i++ ) {
          email = emails[i];
          email.created_at = new Date();
        }

        email_collection.insert(emails, function() {
          callback(null, emails);
        });
      }
    });
};

exports.EmailProvider = EmailProvider;


推荐答案

虽然第一个代码框中的连接代码显示为正确的是,emailProvider对象没有使用它。相反,在app.js中,EmailProvider正在连接到localhost:27017,数据库名称在emailprovider.js中作为'localdb'硬编码。

While the connection code in the first code box appears to be correct, the emailProvider object isn't using it. Instead, in app.js, the EmailProvider is being connected to localhost:27017 and the database name is hardcoded in emailprovider.js as 'localdb'.

你想要什么而是使用EmailProvider中包含主机,端口和数据库名称的MONGOLAB_URI环境变量中提供的连接信息。

What you want to do instead is use the connection information provided in the MONGOLAB_URI environment variable in your EmailProvider, which contains the host, port, and database name already.

有很多方法要做到这一点,但一种方法是将连接代码从第一个代码框移动到EmailProvider构造函数中,然后更改构造函数,以使其使用URI而不是主机和端口。这样,您可以将MONGOLAB_URI变量传递给app.js中的构造函数。

There are a number of ways to go about doing this, but one way would be to move your connection code from that first code box into the EmailProvider constructor, and then change the constructor so that it takes a URI instead of a host and port. That way, you can pass the MONGOLAB_URI variable to the constructor in app.js.

这篇关于如何使用MongoLab在Heroku上设置MongoDB数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆