2017-09-14 66 views
1

我有Node.JS服务器托管在heroku中,我想在我的Meteor应用程序中使用相同的Mongo数据库。如何将我的流星应用程序连接到外部MongoDB?

这里是我的Node.js服务器我Mongo的数据库:

var mongoose  = require('mongoose'); 
var Schema  = mongoose.Schema; 

var messageSchema = new Schema({ 
    requestNumber: String, 
    requestedDateTime: String, 
    reasons: String, 
    state: String, 
    hospital: String, 
    phone: String, 
    status: {type: String, default: 'Pending'}, 
    latestUpdate: Date, 
    createdAt: {type: Date, default: Date.now} 
}); 

module.exports = mongoose.model('Requests', messageSchema); 

这里是我的收藏Meteor

Requests = new Mongo.Collection("requests"); 

Requests.attachSchema(new SimpleSchema({ 
    requestNumber: {type: String}, 
    requestedDateTime: {type: String}, 
    reasons: {type: String}, 
    state: {type: String}, 
    hospital: {type: String}, 
    phone: {type: String}, 
    status: {type: String, defaultValue: 'Pending'}, 
    latestUpdate: {type: Date}, 
    createdAt: {type: Date, defaultValue: Date.now} 
})); 

Requests.allow({ 
    insert: function(userId, doc){ 
    return true; 
    }, 
    update: function(userId, doc, fields, modifier){ 
    return true; 
    }, 
    remove: function(userId, doc){ 
    return true; 
    } 
}); 

这里是我如何连接到我的Node.JS数据库中Meteor应用:

Meteor.startup(() => { 
    process.env.MONGO_URL = 'mongodb://...'; 
}); 

当我在​​中尝试db.requests.find().pretty()时,没有任何内容显示在控制台上。

我在这里做错了什么?

回答

2

我认为这是连接到外部数据库的错误方法。您在之后指定MONGO_URL您的应用程序已启动并且此时它已经启动了内部mongo服务器。

,同时从控制台上运行您的流星应用程序,你应该指定MONGO_URL

MONGO_URL="mongodb://..." meteor 

您可以使用一个微小sh脚本来做到这一点:

#!/bin/sh 
MONGO_URL="mongodb://..." meteor -s <path_to_settings_file> ... <other_parameters> 
+0

感谢。我做了它,它似乎工作,因为我不能使用“meteor mongo”命令,因为它只连接到本地mongo。但我怎样才能确保它连接?我试过“console.log(Requests.find({}))”,但没有任何返回。抱歉花时间。 –

+0

你可以使用'meteor shell'并运行命令,比如'Requests.find()。fetch()' – Styx

+0

我实际上可以在“meteor shell”中看到它,但是在代码里面,Requests.find({}) , 你知道为什么吗? –

相关问题