lastid
lastid

Reputation: 469

Export MongoDB symbol in Package in Meteor 0.6.5

I need to use MongoDB driver in Meteor because I want to use Grid in my application. Before Meteor 0.6.5, I managed to have a mongodb as a package, and it worked fine.

But after the update, with the new Package system, I cannot make it to work. Here is my package.js (in myAppFolder/packages/mongodb/), I also did meteor add mongodb

Package.describe({
    summary: "Mongodb driver"
});

Npm.depends({'mongodb': '1.3.18'});

Package.on_use(function(api){
    MongoDB = Npm.require("mongodb");
    console.log(MongoDB, '--------------');

    api.export('MongoDB', 'server');
});

I can see that the console log prints something when I start the server, but then in my application code at runtime, the value of MongoDB is undefined, the same thing for Package.mongodb.MongoDB . It seems to me that these values are assigned to undefined somehow.

If someone knows how to use the already included MongoDB driver in the mongo-livedata package, it would be a better solution.

Upvotes: 1

Views: 324

Answers (1)

Andrew Mao
Andrew Mao

Reputation: 36900

I don't think it works if you do it in the package.js file; it appears you have to use a separate file. I did something similar to get the csv package, in the following way:

package.js:

Npm.depends({
    csv: "0.3.5"
});

Package.on_use(function (api) {
    api.add_files('server.js', 'server');

    api.export('csv');
});

server.js:

csv = Npm.require('csv');

This is Meteor 0.6.5+ specific. They have a section in the docs about it now: http://docs.meteor.com/#writingpackages

Like you said though, you should be able to Npm.require the same mongodb package that Meteor is already using, and save an additional npm install. For example, the mongo-livedata package exports something called MongoInternals, and you may be able to dig in to it and find out how to pull out the mongo driver:

https://github.com/meteor/meteor/blob/devel/packages/mongo-livedata/package.js

Upvotes: 1

Related Questions