0

I'm trying to establish a connection between my Meteor application and my MongoDB Atlas database.

I have the following bit of JavaScript:

  var MongoClient = require('mongodb').MongoClient, format = require('util').format;                  
MongoClient.connect('<MyMongoURL>', function (err, db) {
if (err) {
    throw err;
} else {
    console.log("successfully connected to the database");

    db.collection('largeTreeMap', function(err, docs) {
    // Check for error
    if(err) return console.log(err);
    // Walk through the cursor
    docs.find().each(function(err, doc) {
        // Check for error
        if(err) return console.err;
        // Log document
        console.log(doc);
    })
});

}
db.close();      });

I added this to a blank JS document called test.js and upon running

node test.js

In my command line it returned the success message and data:

Success

So now that I know the connection can be established I added the code to my Meteor project. I created a basic button and onClick the connection to MongoDB should completed.

However, instead I receive the following console error:

Error

I understand from reading various Stack questions that this is a result of not running npm install mongodb in the project directory. However, I have tried doing this and the terminal returns:

Error 2

Does any body know why the MongoDB is failing to install and preventing me from connecting to MongoDB in my application?

Any help would be much appreciated,

Many thanks,

G

1
  • meteor uses there own version of mongodb internally. Maybe this causing the problem. Commented Jul 17, 2017 at 20:36

1 Answer 1

1

You're trying to connect to the Mongo instance from the client, which is probably not what you want.

The mongodb npm package supports only Node.js, not JavaScript in the browser, as you can see from this line in its package.json

"engines": {
  "node": ">=0.10.3"
},

In the case that worked, you are running it with Node.

What you probably want to do is to set the MONGO_URL environment variable to the Mongo Atlas instance, and leave the implementation of connecting / updating to Meteor itself.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer - Do you know if setting the MONGO_URL in the settings.json file would be enough to connect?
No they are different things - the settings you set in settings.json is accessed via Meteor.settings, this is different from environment variables, which are accessed via process.env.
I am having the same problem when running puppeteer in the browser using browserify, if anyone knows how to solve this, please help.
@coder420 I don't understand your exact use case but running puppeteer on the client seems like a mistake or at least a bad design. puppeteer is typically ran on the server, even if that server is your local development machine. Think of it another way - it'd be a terrible user experience to have the end-user download hundreds of MBs of various browsers to test on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.