Using .find().fetch() from within a function in Meteor


down vote
I am making a project with Meteor and I’m having some issues trying to get data out of mongodb in JavaScript. I have the following in a function:

console.log(Time.find({today: "Saturday", user: Meteor.userId()}).fetch());

This function gets called later down in the code but it returns an empty array. If I run this code in my browsers console it returns an array with 2 objects in it, which is correct. This leads me wondering if I can use the .fetch() function from within my code? As if I leave off the .fetch() it returns what looks like the usual giant object. My real problem is I need the data in the form that .fetch() gives it to me in. Is there any way around this or a fix?


depends where u call it, data are not on client when client side scripts execute yet


I have moved it into a meteor method with some test code to try and get it working. Here is the example code:
Meteor.methods({ getSunday: function(){ return 107; } });


That console code returns undefined, can anyone explain to me why this is?


#4 on the client is always asynchronous and so returns nothing.
You have to add a callback which will have the result you need.


Could you please give me an example of how to get the 107 on my client javascript?

#6"getSunday", function(result) {

Something like this.

Asynchronous code always requires a callback (function(result)), which means "Do this task, and then when it’s done, do this with the parameters that it returns.


Unfortunately still returns undefined :confused: Any ideas?


Try changing the callback’s parameters. I’m only half-awake, might want to try…uh… function(error, result) instead.

It would look like:"getSunday, function(error, result) {
  if (error) {
    console.log("Error: " + error.reason)
  } else {


Yes thanks! I did that right after I replied. That is now showing 107 in the console. Now I’m trying to get it to return 107 but that doesn’t seem to be working now… Not even an error message.


you cant return it, just instead of console.log set some reactive variable which you than use in helper, or is used in template level autorun etc…


So something like this?"getSunday, function(error, result) { if (error) { console.log("Error: " + error.reason) } else { var sundayTime = result; } });
I basically need sundayTime to equal the 107, which it should now. The problem I’m having is I need to use that variable later on in my code but because it is in that function the other function cant see it (Uncaught ReferenceError: sundayTime is not defined)



we still dont see where you call that … so hard to comment


Sorry for the confusion, Thanks to everyone though as I have worked out I can do it through session variables.


It’s a common thing that new people run into. Basically, you either need to define sundayTime outside the callback, then set the value inside (if you wish to use it outside the callback), or any code that uses sundayTime needs to be inside the callback, or sundayTime won’t have a value outside that block of code.