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

down vote
favorite
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:
Server:
Meteor.methods({ getSunday: function(){ return 107; } });

Client:
console.log(Meteor.call("getSunday"));

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

Thanks

Meteor.call 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?

Meteor.call("getSunday", function(result) {
  console.log(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?

1 Like

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

It would look like:

Meteor.call("getSunday, function(error, result) {
  if (error) {
    console.log("Error: " + error.reason)
  } else {
    console.log(result)
  }
});

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?
Meteor.call("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 meteor.call function the other function cant see it (Uncaught ReferenceError: sundayTime is not defined)

Thanks

we still dont see where you call that Meteor.call … 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.