Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Modifying the selector for find/findOne doesn't work as expected on client #39

Closed npvn closed 10 years ago

npvn commented 10 years ago

The problem I'm wanting to solve: I have a collection of orders. Each order has a native _id and a customized orderId (something like S3-9446). I want to prevent other developers from using the wrong selector (e.g. {_id: 'S3-9446'} and {orderId: 's4LP7H6P5DAXrFLwo'} are both wrong). I do this by pre-checking the selector and modify it if necessary: http://screencast.com/t/6lGnvQjLy7tK

As you can see in the screencast, although the selector is correctly edited, the result is still undefined. On server the code works normally and always return the desired object.

Note: In the screencast, App.isWREId will return true if the argument is a customized id (like S3-9446).

mizzao commented 10 years ago

You can't edit the selector by setting it to something else. In order to replace it completely, you will need to set it the arguments object: this.args[0] = newSelector instead of selector = newSelector. For an example, see selector rewriting in https://github.com/mizzao/meteor-partitioner.

An alternative approach is to delete selector.field instead of selector = _.omit(selector, "field"), which won't change the reference to the selector object.

npvn commented 10 years ago

Thanks for the thorough answer, @mizzao.