Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor database connection

Tags:

mongodb

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

But I keep getting a "Exception in template helper: ReferenceError: Boxes is not defined" error - any ideas?

like image 252
JoeTidee Avatar asked Feb 28 '15 22:02

JoeTidee


People also ask

What is Meteor DataBase?

The Visual Meteor DataBase (VMDB) The VMDB contains about 3,000,000 meteors obtained by standardized observing methods which were collected during the last ~25 years.

What is Meteor collection?

Collections are Meteor's way of storing persistent data. The special thing about collections in Meteor is that they can be accessed from both the server and the client, making it easy to write view logic without having to write a lot of server code.

What is Meteor MongoDB?

Meteor provides a complete open source platform for building web and mobile apps in pure JavaScript. The Meteor team chose MongoDB as its datastore for its performance, scalability, and rich features for JSON. Meteor apps run using JavaScript via Node. JS on the server and JavaScript in the phone's browser.

What is Meteor API?

Meteor API Docs. What is Meteor? Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node.js and general JavaScript community.

Where is user data stored in Meteor?

The Meteor.users collection. Meteor comes with a default MongoDB collection for user data. It’s stored in the database under the name users, and is accessible in your code through Meteor.users. The schema of a user document in this collection will depend on which login service was used to create the account.

What is a default connection in Meteor?

By default, clients open a connection to the server from which they’re loaded. When you call Meteor.subscribe, Meteor.status, Meteor.call, and Meteor.apply, you are using a connection back to that default server.

How to call methods on another meteor application using DDP?

Connect to the server of a different Meteor application to subscribe to its document sets and invoke its remote methods. The URL of another Meteor application. To call methods on another Meteor application or subscribe to its data sets, call DDP.connect with the URL of the application. DDP.connect returns an object which provides:


2 Answers

How can you connect to a MongoDB with Meteor?

Scenario A: Use the built-in DB as default

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

Scenario B: Use a different DB as default

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

You can also leave out the user:password@ part of the command if no authentication is needed.

Scenario C: Connect to a second (3rd, etc) DB from the same Meteor app

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

Bonus: Why should you 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. 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.

like image 63
Stephan Avatar answered Sep 29 '22 12:09

Stephan


Based on feedback from saimeunt in the comments above, s/he pointed out that MongoInternals is unavailable to the client portion of a Meteor app. Therefore, the solution was to add in the line "Boxes = new Mongo.Collection("boxes");" to the client logic - here was 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({});
        }
    });
}
like image 40
JoeTidee Avatar answered Sep 29 '22 12:09

JoeTidee