4

I have started learning MeteorJS and made a sample app. I have a collection in mongoDB and I am trying to see that collection in client Here is my server Code(file is in /libs)

newColl=new Meteor.Collection("newColl");
if(Meteor.isServer){
  Meteor.publish('newCollectionData', function(){
     console.log(newColl.find().fetch());
    return newColl.find();
  });
}

Here is My client Code(file is in /client)

  Meteor.subscribe("newCollectionData");
//console.log(newColl.find());
console.log(newColl.find().fetch());
var data= newColl.find().fetch();
console.log(data);

The log in server prints the data correctly but the log on client prints an empty array. PS: I have removed auto publish, but with it also it was giving same result. Where am I going Wrong?

1
  • Try this : Meteor.subscribe("newCollectionData",function(){console.log(newColl.find().fetch());}); Commented Oct 6, 2014 at 22:15

5 Answers 5

4

Cursor.fetch() returns immediately with data currently available. If no data are available on the client in the time of call, it returns nothing.

It is reactive source, so just try to call it in Tracker.autorun and it will be recomputed when the reactive source changes.

Tracker.autorun(function () {
  console.log(newColl.find().fetch());
});
Sign up to request clarification or add additional context in comments.

Comments

3

How to access data in Mongo DB from html ?

First of all you need to have the Mongo DB instance present in global variable i:e it must be declared in any .js file as below. It is not part of client or server code

Say we create a Collection of Events in one of the js files.

    EventList = new Mongo.Collection('Events');

Now, in order to access all the objects from HTML, we would need a helper function inside client in our .js file. Below is Helper used:-

    Template.viewEvent.helpers  ({ 
        //NOTE : 'event' is the name of variable from html template
        'event' : function () {
             //returns list of Objects for all Events
             return EventList.find().fetch();
         }
        'users' : function () {
             //returns reference to Document for all users
             return UserList.find().fetch();
         }

    });

Just left to display all the content on .html:-

Say EventList Collection has fields Event_Name, Event_Date. Below would be the html template code

    <template name="viewEvent">
       <h2>View Event</h2>
       <!-- Iterate on all Objects fetched by the Helper Class-->
       {{#each event}}
          {{Event_Name}} : {{Event_Date}} 
       {{/each}}
   </template>

Comments

1

Try putting

newColl=new Meteor.Collection("newColl");     

into both client side and server side and see if it works?

Also try console.log(newColl.find().fetch()) in your browser console and see if it returns any data. If yes then it may be because the data is not ready yet when you print out the newColl.find().fetch().

Comments

0

I'm new to meteor.js(so maybe I'm wrong). I think you need to implement some methods to observe changes in collection, to avoid that when rendering pages you can use meteor template.

If you want to see a log from the client side only for "learning" purpose, just use client console from your browser.

Comments

0

If the files are correctly named/accesed, try check and add autopublish by

meteor add autopublish

I encountered this error while watching the meteor university contacts tutorial

1 Comment

Your answering a question on meteor from 2014: the framework has changed so much that your answer does not apply to the question asked.

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.