Connect to the Meteor Database

I am trying to connect to my Mongo database, which is located on the machine as a Meteor application. Here are two files in my application:

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({}); } }); } 

But all the time I get the error "Template Exception: Error ReferenceError: Boxes is not defined" - any ideas?

+6
source share
3 answers

How can you connect to MongoDB using Meteor?

Scenario A: Use the default embedded database

This is much simpler than what you did. When you start meteor , you actually start the database with the Meteor server, where Meteor listens on port 3000 and the database on port 3001. The Meteor application is already connected to this database on port 3001 and uses db called meteor . No need to return to MongoInternals.RemoteCollectionDriver . Just delete this code and change it to:

  Boxes = new Mongo.Collection("boxes"); // use default MongoDB connection 

Scenario B: Use a different default database

Using the MONGO_URL environment MONGO_URL , you can set the connection string in MongoDB when starting the Meteor server. Instead of a local database 3001 or an unauthenticated connection, you can specify exactly where and how to connect. Launch your Meteor server as follows:

 $ MONGO_URL=mongodb://user: password@localhost :27017/meteor meteor 

You can also leave the user: password@ command if authentication is not required.

Scenario C: connect to the second (3rd, etc.) database from the same Meteor application

Now we need to use MongoInternals.RemoteCollectionDriver . If you want to use a different database that is not the base database defined when the Meteor server starts, you should use your approach.

 var database = new MongoInternals.RemoteCollectionDriver('mongodb://user: password@localhost :27017/meteor'); var numberOfDocs = database.open('boxes').find().count(); 

Bonus: why you should not use MongoInternals with 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. This means that if you use DDP.connect to connect to another server, you can use your code, but you should not mix MongoInternals with Mongo.Collection - they work poorly together.

+16
source

Based on the saimeunt reviews in the comments above, he / he indicated that MongoInternals is not available for the Meteor client side. Therefore, the solution was to add to the line "Boxes = new Mongo.Collection (" boxes "); to the client logic - here is the final working solution:

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) { Boxes = new Mongo.Collection("boxes"); Meteor.subscribe('boxes'); Template.homeCanvasTpl.helpers({ boxes: function () { return Boxes.find({}); } }); } 
+1
source

Meteor has 2 different environments: a server environment running on Node.JS and a client environment running on browsers.

In your code, you declare the Boxes Mongo collection only in a server environment, you need to accept this declaration from the Meteor.isServer condition (and BTW do not use them, separate your code in server/ , client/ and lib/ ).

Also, not sure if you need to connect to your MongoDB this way, maybe you should examine the MONGO_URL environment MONGO_URL , which it probably already does, what do you need? (specify the URL of the mongo connection on the remote (or local) Mongo server).

0
source

Source: https://habr.com/ru/post/983162/


All Articles