Skip to content

Commit 2c4436f

Browse files
flovilmarthramos
authored andcommitted
Adds examples for usage of beforeFind (#376)
* Adds examples for usage of beforeFind * Update cloud-code.md Fix italic for note * Update cloud-code.md
1 parent 2d8a7ea commit 2c4436f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

_includes/cloudcode/cloud-code.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,57 @@ The `afterDelete` handler can access the object that was deleted through `reques
319319
The client will receive a successful response to the delete request after the handler terminates, regardless of how it terminates. For instance, the client will receive a successful response even if the handler throws an exception. Any errors that occurred while running the handler can be found in the Cloud Code log.
320320

321321
If you want to use `afterDelete` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User](https://parse.com/docs/js/api/symbols/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself.
322+
323+
# beforeFind Triggers
324+
325+
*Available only on parse-server cloud code starting 2.2.20*
326+
327+
In some cases you may want to transform an incoming query, adding an additional limit or increasing the default limit, adding extra includes or restrict the results to a subset of keys. You can do so with the `beforeFind` trigger.
328+
329+
330+
## Examples
331+
332+
```javascript
333+
// Properties available
334+
Parse.Cloud.beforeFind('MyObject', function(req) {
335+
let query = req.query; // the Parse.Query
336+
let user = req.user; // the user
337+
let triggerName = req.triggerName; // beforeFind
338+
let isMaster = req.master; // if the query is run with masterKey
339+
let logger = req.log; // the logger
340+
let installationId = req.installationId; // The installationId
341+
});
342+
343+
// Selecting keys
344+
Parse.Cloud.beforeFind('MyObject', function(req) {
345+
let query = req.query; // the Parse.Query
346+
// Force the selection on some keys
347+
query.select(['key1', 'key2']);
348+
});
349+
350+
// Asynchronous support
351+
Parse.Cloud.beforeFind('MyObject', function(req) {
352+
let query = req.query;
353+
return aPromise().then((results) => {
354+
// do something with the results
355+
query.containedIn('key', results);
356+
});
357+
});
358+
359+
// Returning a different query
360+
Parse.Cloud.beforeFind('MyObject', function(req) {
361+
let query = req.query;
362+
let otherQuery = new Parse.Query('MyObject');
363+
otherQuery.equalTo('key', 'value');
364+
return Parse.Query.or(query, otherQuery);
365+
});
366+
367+
// Rejecting a query
368+
Parse.Cloud.beforeFind('MyObject', function(req) {
369+
// throw an error
370+
throw new Parse.Error(101, 'error');
371+
372+
// rejecting promise
373+
return Promise.reject('error');
374+
});
375+
```

0 commit comments

Comments
 (0)