I am new to Meteorjs and I am trying to retrieve data from an already existing MongoDB. Heres what I have so far:
I set the env variable MONGO_URL to the mongoDB url export MONGO_URL="mongodb://username:password@address:port/dbname"
Created a new meteor project with the following code:
MyCollection = new Meteor.Collection('mycollection'); if (Meteor.isClient) { //Meteor.subscribe("mycollection"); console.log(MyCollection.findOne()); Template.hello.greeting = function () { return MyCollection.findOne(); }; } if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup console.log(MyCollection.findOne()); }); }
I know the server side console.log(MyCollection.findOne());
works as it prints out the correct data on the terminal.
The problem is with the client side. When I view the page on my browser, the data is blank and console.log(MyCollection.findOne());
shows 'undefined'.
I know that autopublish is on and I dont have to manually publish the collection from the server side.
I would like to know how I could make the client read from my external mongoDB directly. Let me know if you have any suggestions!