0
votes

I need to develop a Meteor app using external MongoDB, but I'm not sure how to connect and fetch external Mongo data using Meteor.

The following is what I've tried.


Fetch external MongoDB data in the shell (success)

I can connect into external MongoDB under Terminal (use database sm_app):

$ mongo <USER>:<PASSWORD>@<SERVER>:<PORT>/sm_app

and fetch the only one data:

> db.servertop10.findOne()

It shows:

{
  "_id": ObjectId("......")
  "list": [
    ...
  ] 
}

Fetch external MongoDB data in Meteor (failed)

Now I try running Meteor with external MongoDB on my Mac:

MONGO_URL=mongodb://<USER>:<PASSWORD>@<SERVER>:<PORT>/sm_app meteor run

then try to fetch data in Meteor.js:

import { Mongo } from 'meteor/mongo';

const ServerMonitor = new Mongo.Collection('servertop10');
console.log('findOne:', ServerMonitor.findOne());

but it shows:

findOne: undefined

Questions

  1. How can I fetch the data under Meteor.js?
  2. Is it possible using other databases in Meteor.js with the same external MongoDB? How to do this?

Edit

Previously, I only tried fetch data on the front end. Now I try logging the results on both server side and client side.

// Import this file on the main.js files of both server and client
import { Mongo } from 'meteor/mongo';

const ServerMonitor = new Mongo.Collection('servertop10');
Meteor.startup(() => {
  console.log('MONGO_URL:', process.env.MONGO_URL);
  console.log('findOne:', ServerMonitor.findOne());
}

The server side shows:

> MONGO_URL: mongodb://<USER>:<PASSWORD>@<SERVER>:<PORT>/sm_app
> findOne: {
  "_id": ObjectId("......")
  "list": [
    ...
  ] 
}

but the client side shows:

> MONGO_URL: undefined
> findOne: undefined

I still failed to fetch the data in the render() of a React component:

render() {
  // still print `undefined`
  console.log('findOne:', ServerMonitor.findOne());
  return (<h1>Hello</h1>);
}

but I can fetch the data on the console of the browser (with autopublish):

> ServerMonitor.findOne()
  Object {_id: ..., ...}

I don't know why I can't fetch the data on the client side.

2
Meteor.startup(() => console.log(process.env.MONGO_URL)) prints undefined - Xaree Lee
Make sure you use export if you are on Mac or Linux - Mikkel
I tried to print the process.env.MONGO_URL on the server side and fetch the data, and it worked well. When I tried to fetch data on the client side using the same collection code, I got nothing. I have no idea about this. Please see the new edit on the post. - Xaree Lee
It takes some time for server to send data to client. You see undefined on client console because at that time server has not finished sending data yet - kkkkkkk

2 Answers

0
votes

check with

MONGO_URL="mongodb://<USER>:<PASSWORD>@<SERVER>:<PORT>/sm_app" meteor

Also if you are not using package autopublish and insecure then you will have to use publish and subscribe methods.

0
votes

Thanks Khang and Mikkel, I finally resolve this problem.

If you encounter the same problem, please check:

  • Use export for external Mongo with URL (MONGO_URL).
  • If you use React with Meteor, make sure use createContainer from react-meteor-data to trigger the callback when the database is up or its data changes.

After using export for MONGO_URL, I find that I still can't fetch data in client side, but it works in the server side.

This is because I use React with Meteor, I forgot to use createContainer derived from react-meteor-data to connect the data changes from mini-Mongo collections to React components. Logging fetch results in React's render() or Meteor.startup() will print undefined until the callback passed to createContainer was triggered by database changes.