Async publish function

I got a problem with publications and async functions. I started updating my App Framework to Meteor 3.0 by switching to planning:roles 4, wich now does not allow for sync functions server-side (duh). Meteor is still at 2.16.

So userWithIdIsInRole has to be async now, wich in turn makes the whole publish function async.

Meteor.publish(`${sourceName}.rows`, async function({search, query, queryUiObject, sort, limit, skip}) {
  if (!async(userWithIdIsInRole({
    id: this.userId,
    role: viewTableRole
  }))) {
    return this.ready();
  }
  return this.autorun(function(computation) {
    var pipeline;
    pipeline = getRowsPipeline({
      pub: this,
      search,
      query,
      queryUiObject,
      sort,
      limit,
      skip
    });
    return ReactiveAggregate(this, collection, pipeline, {
      clientCollection: `${sourceName}.rows`,
      debounceDelay: debounceDelay,
      noAutomaticObservers: noAutomaticObserves,
      observers: typeof observers !== "undefined" && observers !== null ? observers : []
    });
  });
});

This of course leads to this Error:

Exception from sub users.rows id S2H8rLwbcqk6EvwZB Error: Publish function can only return a Cursor or an array of Cursors

I have no Idea how to solve this…

1 Like

This appears to be a crucial use case that was missing. I had wondered about it in the past, and your report provided the answer. It doesn’t seem supported, but I think it should be. I am talking with the team to bring this up.

Also, I know this isn’t related to the issue, but this.autorun from peerlibrary/meteor-reactive-publish won’t work as it doesn’t support 3.0. I love this library and, after the official Meteor 3.0 release, I might try migrating it if nobody else does.

Actually, we have async publication behavior on test coverage. So Meteor 3.0 seems to address async publication code as shown here.

The issue might be with the surrounding code. Try simplifying it, avoid using this.autorun , and don’t use return this.ready() and instead just run this.ready() and return nothing or just null. Your issue might be come from any of these.

1 Like

Thanks. I tried your suggestions. Removing this.autorun did it. I had that in there since way before COVID , I think tunguska:reactive-aggregate probably required it back then. Seems it doesn’t anymore :wink:

Just running this.ready() without returning it does not work (it does not return an empty publication when the user should not get any data).