Closed ziedmahdi closed 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 else
statement with the following:
} else {
this.ready();
return;
}
You can even omit the return;
statement.
// 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()
).
@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.
@ziedmahdi Let me know if that PR does not work in your case.
@johanbrook Let me know if you need more details.
When testing the published data for this collection,
userId = null
, I getPublish function can only return a Cursor or an array of Cursors
I think the collector should expect empty arrays and
null
values.