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


#1

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?


#2

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


#3

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


#4

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.


#5

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


#6
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.


#7

Unfortunately still returns undefined :confused: Any ideas?


#8

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)
  }
});

#9

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.


#10

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…


#11

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


#12

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


#13

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


#14

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.


#15