0

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.

5
  • 2
    check this: stackoverflow.com/a/40713857/2845061 Commented Nov 22, 2016 at 2:27
  • Meteor.startup(() => console.log(process.env.MONGO_URL)) prints undefined Commented Nov 22, 2016 at 2:32
  • Make sure you use export if you are on Mac or Linux Commented Nov 22, 2016 at 5:02
  • 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. Commented Nov 22, 2016 at 5:44
  • 1
    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 Commented Nov 22, 2016 at 7:54

2 Answers 2

0

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.

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

2 Comments

Please see my new edit. I can now fetch data on server side, but not on client side with autopublish.
If the data can be fetched on server side you can create a method call from client side to get the data from server side. [link]guide.meteor.com/methods.html
0

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.

Comments

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.