Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publication returns [] but collection is undefined #5

Closed
hoolymama opened this issue Aug 30, 2016 · 6 comments
Closed

publication returns [] but collection is undefined #5

hoolymama opened this issue Aug 30, 2016 · 6 comments

Comments

@hoolymama
Copy link

Hi,

I have a publication that returns a cursor. If the collection exists but the cursor has no documents, I would expect publication-collector have an empty array for that collection. Instead it is undefined.

Error: Cannot read property 'length' of undefined

If I add one document its fine.

Is this behavior expected?

Here's my publication:

Meteor.publish("pairsAsParticipant", function (participantId) {
  authorizeOwner.call(this, participantId);
  return Pairs.find( { $and:   [ {"inviteeId":{$ne:null}},  { $or: [ { inviterId: participantId }, { inviteeId: participantId } ] } ]  } );
});

and my test:

  describe('No Pairs', () => {
    beforeEach(function (){
      resetDatabase()
      // make 5 pairs
      const fiveRandomPairs = [...Array(5)].map( () => {
        return Factory.create('pair', { inviterId: Random.id(), inviteeId: Random.id()  })
      })
    })

    it('should find zero pairs if currentUser is not a participant', function() {
      const collector = new PublicationCollector({userId: currentUserId})
      collector.collect('pairsAsParticipant', currentUserId,  (collections) => {
        expect(collections.pairs).to.have.lengthOf(0)
      })
    })
  })
@johanbrook
Copy link
Collaborator

True, I agree. Investigating.

@hoolymama
Copy link
Author

hoolymama commented Sep 1, 2016

Hi there,

I'm sorry, I didn't realise I had hit "send" on this. I intended to investigate further, because maybe your code behaves the same as a Meteor subscription.

In the meantime I changed my test to this:

expect(collections.pairs).to.not.exist

@johanbrook
Copy link
Collaborator

Well, if you call .fetch() on a collection with no documents, it'll return an empty array, not undefined. I still think you had the correct reasoning at the top.

johanbrook added a commit that referenced this issue Sep 1, 2016
This ensures that even if a publication isn't returning
any documents from its cursor, we still have a key in the
resulting `collections` object in the collector's callback.
@hoolymama
Copy link
Author

Yes true. (I was confused earlier). Thanks for fixing it!

@johanbrook
Copy link
Collaborator

Fixed in #7.

@shelagh-lewins-ucl
Copy link

Hi, I seem to be seeing this problem again. When my publish function returns nothing::

this.ready();
return;

collections.myCollection is undefined. I'd expect it to be an empty array.

I also tried

return [];

but the test still sees undefined.

Have I missed something or has this bug reappeared?

versions:
johanbrook:publication-collector@1.1.0
METEOR@1.6.1

Thank you for this awesome package, it's really good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants