0

I'm learning Meteor and I was trying to pass the result of a Collection.find() into and array (using a variable) and the simpler code I have is (in a file that is in the root):

    CalEvents = new Mongo.Collection('calevents');    //creating a collection 

/*------------------------- Populating the database with dummy data-------*/
    if (Meteor.isServer) {                            
    Meteor.startup(function () {
        if (CalEvents.find().count() === 0) {
          CalEvents.insert({
                title: "Initial room",
                start: '2010-02-02'
          });
        }
      });
    }
  /*--------------- Creating an array from the collection-----------------*/
    events = [];
    calEvents = CalEvents.find({});
    calEvents.forEach(function(evt){
        events.push({
            title: evt.title,
            start: evt.start,
        })
    });

The page has nothing to show but using the console I can see (CalEvents.find().fetch()) that I have data in my database but the "events" variable is empty... I can't understand why because I tried several other things such as changing file names and moving code to guarantee the proper order. And I already tried to use CalEvents.find().fetch() to create an array an put the result into a variable but I'm not able to do it... Does anyone know what's so simple that I'm missing?...

Paulo Janeiro
  • 3,181
  • 4
  • 28
  • 46

1 Answers1

0

Do you use autosubscribe?

You probably need to make sure the sbscription is ready. See Meteor: How can I tell when the database is ready? and Displaying loader while meteor collection loads.

The reason you do see CalEvents.find().fetch() returning items in the console is that by the time you make that call, the subscription is ready. But in your events = []; ... code (which I assume is in a file under the client directory, you might have assumed that the subscription data has arrived when in fact it has not.

A useful debugging tool is Chrome's device mode ("phone" icon near the search icon in DevTools), which lets you simulate slow networks (e.g. GPRS, with 500ms delay for every request).

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • Hi Dan! Yes I had autopublish. And I removed it and changed the array code to inside a function used as a callback in the subscription and it worked! Do you think that using Iron Router WaitOn and other will make it easy to deal with this or ...Otherwise we'll have to pay attention toa almost ALL the subscriptions no? Thank you! – Paulo Janeiro Feb 05 '15 at 21:41
  • Yes, iron:router's waitOn makes dealing with subscriptions easier. I recommend reading [understanding Meteor's publish/subscribe](http://stackoverflow.com/a/21853298/1269037) and [template-level subscriptions](https://www.discovermeteor.com/blog/template-level-subscriptions/) (even better than iron:router's approach in many cases). Also, you're welcome, feel free to upvote/accept the answer if useful, and remember to pick a StackOverflow username more descriptive than user3481111 :) – Dan Dascalescu Feb 05 '15 at 22:55
  • You're right Dan...it seems that without a proper name I can't upvote :( ...but I already accepted your answer (I think). Thank you again, – Paulo Janeiro Feb 05 '15 at 23:10
  • @user3481111: have a look at http://tinyurl.com/so-username. It's a post by the most famous StackOverflow user of all time, Jon Skeet. – Dan Dascalescu Feb 05 '15 at 23:17