Micha Roon
Micha Roon

Reputation: 4009

using Future.wait() in Meteor.methods

Running subscriptions in async mode with new Future() works fine but if the same thing is done in a Meteor.method the application crashes with a message that it can not wait without a fiber. But I have to return something from the Meteor.method

This is the method:

/**global variables*/
var Fiber = Npm.require("fibers");
var Future = Npm.require("fibers/future");

Meteor.methods({
'single-data': function (form, state, tenant, selectedRow, search, sort) {
  var user = Meteor.users.findOne({_id: this.userId});
  /**check if the user can see the data in the state*/
  if (!(isAdmin(user, form) || hasPermission(user, form, state, "read"))) {
     return null;
  }

  /** set the sort order for the query*/
  var order = [];
  if (sort) {
     order.push(["fieldData." + sort.column + ".0.value", sort.order == 1 ? "asc" : "desc"]);
  }
  order.push(["submitTime", "desc"]);

  var query = dataQuery(form, state, _.uniq(_.pluck(user.groups, "tenant")), search);

  var fut = new Future();
     var fut = new Future();   setTimeout(function () {
        fut.ret(FormDatas.find(query, {sort: order, skip: selectedRow, limit: 1}).fetch()[0]);
     }, 0 * 1000);

  // Wait for async to finish before returning the result
  return fut.wait();
}
});

Upvotes: 0

Views: 1140

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

I believe that the return value from future should not contain asynchronous code, it should happen before calling ret(). Anyway, in your case future is not needed, simply call

return FormDatas.findOne(query, options);

Upvotes: 0

Related Questions