Meteor-Community-Packages / meteor-publication-collector

Test a Meteor publication by collecting its output.
https://atmospherejs.com/johanbrook/publication-collector
MIT License
33 stars 20 forks source link

Exception when publication returns an empty array. #28

Closed ziedmahdi closed 7 years ago

ziedmahdi commented 7 years ago

When testing the published data for this collection, userId = null, I get Publish function can only return a Cursor or an array of Cursors

Meteor.publish('kibanaDashboards', function () {
    //make sure that the user is logged in
    if (this.userId) {
        return KibanaDashboards.find({});
    } else {
        this.stop();
        return [];
    }
});

I think the collector should expect empty arrays and null values.

nkahnfr commented 7 years ago

Hi,

The error is expected as you must only return a cursor or an array of cursors (see related Meteor documentation).

You can fix your code by replacing the elsestatement with the following:

} else {
    this.ready();
    return;
}

You can even omit the return; statement.

ziedmahdi commented 7 years ago

so in related docs

// Sometimes publish a query, sometimes publish nothing.
Meteor.publish('secretData', function () {
  if (this.userId === 'superuser') {
    return SecretData.find();
  } else {
    // Declare that no data is being published. If you leave this line out,
    // Meteor will never consider the subscription ready because it thinks
    // you're using the `added/changed/removed` interface where you have to
    // explicitly call `this.ready`.
    return [];
  }
});

so a publication can return an empty array. Also, stopping a publication (this.stop()) is not same as marking it as ready (this.ready()).

nkahnfr commented 7 years ago

@ziedmahdi OK, I checked the source code and an empty array should indeed be supported as a return value for a publication. Basically it should only state that the publication is ready.

That said, IMHO your PR is still incorrect due to this.ready(); having been moved outside the if statement dealing with publications that return cursors.

I will work on a PR that will fix the empty array use-case without breaking the low-level interface one.

nkahnfr commented 7 years ago

@ziedmahdi Let me know if that PR does not work in your case.

@johanbrook Let me know if you need more details.