Search code examples
javascriptmeteor-blaze

Meteor client-side database query behavior


I noticed somehow weird behavior when followed Meteor introductory tutorial. The tutorial used this snippet to query the database from the client-side code:

Template.body.helpers({
    tasks () {
        return Tasks.find({});
    },
});

It should return a cursor to the found collections then we can get the actual data using .fetch() and it works as expected. but whenever i try to query in 'main.js' file on the client using something like this:

console.log(Tasks.find({}).fetch());

it always prints an empty array! any explanation ?

NOTE: when i query in the server-side code or the Template helper context (which is considered a client-side code) it prints all the data in the database perfectly!


Solution

  • The client-side collection is probably empty at the time the query is evaluated. Top-level statements main.js can run before the client receives all if its collections data from the server.