流星数据库连接 [英] Meteor database connection
问题描述
我试图连接到我的Mongo数据库,该数据库作为Meteor应用程序位于计算机上.这是我的应用程序中的两个文件:
I am trying to connect to my Mongo database which is situated on the machine as my Meteor app. Here are two files in my app:
a.js:
if (Meteor.isServer) {
var database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/meteor");
Boxes = new Mongo.Collection("boxes", { _driver: database });
Meteor.publish('boxes', function() {
return Boxes.find();
});
}
b.js:
if (Meteor.isClient) {
Meteor.subscribe('boxes');
Template.homeCanvasTpl.helpers({
boxes: function () {
return Boxes.find({});
}
});
}
但是我不断收到模板帮助器中的异常:ReferenceError:未定义框"错误-有什么想法吗?
But I keep getting a "Exception in template helper: ReferenceError: Boxes is not defined" error - any ideas?
推荐答案
如何使用流星连接到MongoDB?
How can you connect to a MongoDB with Meteor?
方案A:默认使用内置数据库
这比您所做的要简单得多.当您运行meteor
时,实际上是使用Meteor服务器启动一个数据库,其中Meteor在端口3000上侦听,而数据库在端口3001上侦听.Meteor应用程序已在端口3001上连接到该数据库,并使用名为meteor
的数据库.完全没有必要退回到MongoInternals.RemoteCollectionDriver
.只需删除该代码并将其更改为:
This is much simpler than what you did. When you run meteor
you actually start a DB with the Meteor server, where Meteor listens on port 3000 and the database on port 3001. The Meteor app is already connected to this database at port 3001 and uses a db named meteor
. There is no need whatsoever to fall back to MongoInternals.RemoteCollectionDriver
. Just remove that code and change things to:
Boxes = new Mongo.Collection("boxes"); // use default MongoDB connection
方案B:默认使用其他数据库
使用MONGO_URL
环境变量,可以在启动Meteor服务器时将连接字符串设置为MongoDB.您可以确切指定连接的位置和方式,而不是本地端口3001数据库或未经身份验证的连接.像这样启动您的Meteor服务器:
Using the MONGO_URL
environment variable you can set the connection string to a MongoDB when starting the Meteor server. Instead of the local port 3001 database or an unauthenticated connection you can specify exactly where and how to connect. Start your Meteor server like this:
$ MONGO_URL=mongodb://user:password@localhost:27017/meteor meteor
如果不需要身份验证,您也可以省略该命令的user:password@
部分.
You can also leave out the user:password@
part of the command if no authentication is needed.
方案C:从同一个Meteor应用程序连接到第二个(第3个,等等)数据库
现在我们需要使用MongoInternals.RemoteCollectionDriver
.如果您希望使用另一个不是启动Meteor服务器时定义的默认数据库的数据库,则应该使用您的方法.
Now we need to use MongoInternals.RemoteCollectionDriver
. If you wish to use another database that is not the default DB defined upon starting the Meteor server you should use your approach.
var database = new MongoInternals.RemoteCollectionDriver('mongodb://user:password@localhost:27017/meteor');
var numberOfDocs = database.open('boxes').find().count();
奖金:为什么不将MongoInternals
与Mongo.Collection
一起使用?
Bonus: Why should you not use MongoInternals
with Mongo.Collection
?
文档表明,您不应将任何Mongo连接传递给new Mongo.Collection()
命令,而只能将连接到另一个Meteor实例.这意味着,如果您使用DDP.connect
连接到其他服务器,则可以使用您的代码-但您不应将MongoInternals
与Mongo.Collection
混合使用-它们不能很好地协同工作.
As the docs indicate you should not pass any Mongo connection to the new Mongo.Collection()
command, but only a connection to another Meteor instance. That means, if you use DDP.connect
to connect to a different server you can use your code - but you shouldn't mix the MongoInternals
with Mongo.Collection
- they don't work well together.
这篇关于流星数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!