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

Return empty object or null cursor #18

Closed
fongandrew opened this issue Nov 5, 2014 · 9 comments
Closed

Return empty object or null cursor #18

fongandrew opened this issue Nov 5, 2014 · 9 comments
Labels

Comments

@fongandrew
Copy link

Is there a way for a find function to return an intentionally empty set of documents? For example, I have a parent publication retrieving the User document and a child find function returning a cursor only if the User document contains the requisite permission.

If the child find function is passed an invalid User document, there's currently no good way to process that. When using Meteor.publish, we can return an empty set of values as an empty Array or simply by calling this.ready() if the parent subscription doesn't check out. That don't appear to work within Meteor.publishComposite and result in the follow traceback instead:

I20141105-22:52:42.905(0)? Exception from task: TypeError: Object  has no method '_getCollectionName'
I20141105-22:52:42.907(0)?     at Publication._getCollectionName (packages/reywood:publish-composite/publish_composite.js:171)
I20141105-22:52:42.908(0)?     at Publication.publish (packages/reywood:publish-composite/publish_composite.js:105)
I20141105-22:52:42.909(0)?     at null.<anonymous> (packages/reywood:publish-composite/publish_composite.js:180)
I20141105-22:52:42.910(0)?     at Array.forEach (native)
I20141105-22:52:42.910(0)?     at Function._.each._.forEach (packages/underscore/underscore.js:105)
I20141105-22:52:42.911(0)?     at Publication._publishChildrenOf (packages/reywood:publish-composite/publish_composite.js:177)
I20141105-22:52:42.911(0)?     at Object.observeHandle.cursor.observe.added (packages/reywood:publish-composite/publish_composite.js:118)
I20141105-22:52:42.912(0)?     at observeChangesCallbacks.added (packages/minimongo/observe.js:153)
I20141105-22:52:42.913(0)?     at self.applyChange.added (packages/minimongo/observe.js:53)
I20141105-22:52:42.914(0)?     at packages/mongo/observe_multiplex.js:188
I20141105-22:52:42.944(0)? Exception from sub PLdN4pDMc5NfWi82h TypeError: Object  has no method '_getCollectionName'
I20141105-22:52:42.944(0)?     at Object.Future.wait (/home/vagrant/.meteor/packages/meteor-tool/.1.0.35.ftql1v++os.linux.x86_64+web.browser+web.cordova/meteor-tool-os.linux.x86_64/dev_bundle/lib/node_modules/fibers/future.js:326:15)
I20141105-22:52:42.945(0)?     at _.extend.runTask (packages/meteor/fiber_helpers.js:79)
I20141105-22:52:42.946(0)?     at _.extend.addHandleAndSendInitialAdds (packages/mongo/observe_multiplex.js:47)
I20141105-22:52:42.946(0)?     at MongoConnection._observeChanges (packages/mongo/mongo_driver.js:1108)
I20141105-22:52:42.947(0)?     at Cursor.observeChanges (packages/mongo/mongo_driver.js:783)
I20141105-22:52:42.947(0)?     at Function.LocalCollection._observeFromObserveChanges (packages/minimongo/observe.js:177)
I20141105-22:52:42.947(0)?     at Cursor.observe (packages/mongo/mongo_driver.js:777)
I20141105-22:52:42.948(0)?     at Publication.publish (packages/reywood:publish-composite/publish_composite.js:108)
I20141105-22:52:42.948(0)?     at null._handler (packages/reywood:publish-composite/publish_composite.js:12)
I20141105-22:52:42.948(0)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599)
I20141105-22:52:42.949(0)?     - - - - -
I20141105-22:52:42.950(0)?     at Publication._getCollectionName (packages/reywood:publish-composite/publish_composite.js:171)
I20141105-22:52:42.951(0)?     at Publication.publish (packages/reywood:publish-composite/publish_composite.js:105)
I20141105-22:52:42.951(0)?     at null.<anonymous> (packages/reywood:publish-composite/publish_composite.js:180)
I20141105-22:52:42.952(0)?     at Array.forEach (native)
I20141105-22:52:42.953(0)?     at Function._.each._.forEach (packages/underscore/underscore.js:105)
I20141105-22:52:42.954(0)?     at Publication._publishChildrenOf (packages/reywood:publish-composite/publish_composite.js:177)
I20141105-22:52:42.955(0)?     at Object.observeHandle.cursor.observe.added (packages/reywood:publish-composite/publish_composite.js:118)
I20141105-22:52:42.956(0)?     at observeChangesCallbacks.added (packages/minimongo/observe.js:153)
I20141105-22:52:42.957(0)?     at self.applyChange.added (packages/minimongo/observe.js:53)
I20141105-22:52:42.958(0)?     at packages/mongo/observe_multiplex.js:188
@reywood
Copy link
Collaborator

reywood commented Nov 5, 2014

The find function can return any falsy value (e.g., null, undefined, false, etc) to short circuit. This effectively returns an empty set. You should never call this.ready() or this.stop() in find functions.

For example:

Meteor.publishComposite("mypub", {
    find: function() {
        if (!this.user) {
            return;
        }

        return MyCollection.find({ ... });
    }
});

@reywood reywood closed this as completed Nov 5, 2014
@bitomule
Copy link

bitomule commented Nov 6, 2014

And what happens if you didn't return nothing? Can that cause problems?

@reywood
Copy link
Collaborator

reywood commented Nov 6, 2014

Doing return; is equivalent to return undefined; which is perfectly fine.

@bitomule
Copy link

bitomule commented Nov 6, 2014

Yeah, but what happens if a find doesn't return anything, like:

Find:{
   If(bla){
       return find...
   }
}

@reywood
Copy link
Collaborator

reywood commented Nov 6, 2014

Collection.find() always returns a cursor, so that's fine. It doesn't matter if there are records in it or if it's empty.

@bitomule
Copy link

bitomule commented Nov 6, 2014

Sorry, maybe I didn't explain it well but my question was what happens if bla (in this case) is false so collection.find is never fired. No return is called.

@reywood
Copy link
Collaborator

reywood commented Nov 6, 2014

Ah, I see. That is also equivalent to return undefined; so no problem.

@bitomule
Copy link

ok thanks :)

@s-devaney
Copy link

In the native Meteor publish method, returning an empty cursor (i.e. []) works (although this.stop() is best practice) but doing this in publishComposite results in an error, it might be good to replicate this behavior? Although a return statement works. as described above.

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

No branches or pull requests

4 participants