Skip to content

Commit

Permalink
Replace response.success / response.error (parse-community#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy authored Oct 20, 2020
1 parent e47a6e4 commit 02030f7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 47 deletions.
4 changes: 2 additions & 2 deletions _includes/arduino/cloud-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. We provi
For example, you define a Cloud Function as below.

```javascript
Parse.Cloud.define("hello", function(request, response) {
response.success(request.body);
Parse.Cloud.define("hello", request => {
return request.body;
});
```

Expand Down
2 changes: 0 additions & 2 deletions _includes/common/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ The following is a list of all the error codes that can be returned by the Parse
| `ScriptFailed` | 141 | Cloud Code script failed. Usually points to a JavaScript error. Check error message for more details. |
| `FunctionNotFound` | 141 | Cloud function not found. Check that the specified Cloud function is present in your Cloud Code script and has been deployed. |
| `JobNotFound` | 141 | Background job not found. Check that the specified job is present in your Cloud Code script and has been deployed. |
| `SuccessErrorNotCalled` | 141 | success/error was not called. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job never reaches either of the success/error methods, this error will be returned. This may happen when a function does not handle an error response correctly, preventing code execution from reaching the success() method call. |
| `MultupleSuccessErrorCalls` | 141 | Can't call success/error multiple times. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job calls success() and/or error() more than once in a single execution path, this error will be returned. |
| `ValidationFailed` | 142 | Cloud Code validation failed. |
| `WebhookError` | 143 | Webhook error. |
| `InvalidImageData` | 150 | Invalid image data. |
Expand Down
23 changes: 9 additions & 14 deletions _includes/common/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -1008,19 +1008,15 @@ You can use this to offload processing to the Parse servers thus increasing your
We saw examples of limiting the data returned by writing restrictive queries. You can also use [Cloud Functions]({{ site.baseUrl }}/cloudcode/guide/#cloud-functions) to help limit the amount of data returned to your app. In the following example, we use a Cloud Function to get a movie's average rating:

```javascript
Parse.Cloud.define("averageStars", function(request, response) {
var Review = Parse.Object.extend("Review");
var query = new Parse.Query(Review);
Parse.Cloud.define("averageStars", async (request) => {
const query = new Parse.Query("Review");
query.equalTo("movie", request.params.movie);
query.find().then(function(results) {
var sum = 0;
for (var i = 0; i < results.length; ++i) {
sum += results[i].get("stars");
}
response.success(sum / results.length);
}, function(error) {
response.error("movie lookup failed");
});
const results = await query.find();
let sum = 0;
for (let i = 0; i < results.length; ++i) {
sum += results[i].get("stars");
}
return sum / results.length;
});
```

Expand Down Expand Up @@ -1308,7 +1304,7 @@ Let's walk through an example of how you could build an efficient search. You ca

```javascript
var _ = require("underscore");
Parse.Cloud.beforeSave("Post", function(request, response) {
Parse.Cloud.beforeSave("Post", request => {
var post = request.object;
var toLowerCase = function(w) { return w.toLowerCase(); };
var words = post.get("text").split(/\b/);
Expand All @@ -1321,7 +1317,6 @@ Parse.Cloud.beforeSave("Post", function(request, response) {
hashtags = _.map(hashtags, toLowerCase);
post.set("words", words);
post.set("hashtags", hashtags);
response.success();
});
```

Expand Down
18 changes: 5 additions & 13 deletions _includes/common/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,10 @@ One particularly common use case for Cloud Code is preventing invalid data from
To create validation functions, Cloud Code allows you to implement a `beforeSave` trigger for your class. These triggers are run whenever an object is saved, and allow you to modify the object or completely reject a save. For example, this is how you create a [Cloud Code beforeSave trigger]({{ site.baseUrl }}/cloudcode/guide/#beforesave-triggers) to make sure every user has an email address set:

```js
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
var user = request.object;
Parse.Cloud.beforeSave(Parse.User, request => {
const user = request.object;
if (!user.get("email")) {
response.error("Every user must have an email address.");
} else {
response.success();
throw "Every user must have an email address.";
}
});
```
Expand All @@ -548,17 +546,11 @@ Say you want to allow a user to "like" a `Post` object without giving them full
The master key should be used carefully. setting `useMasterKey` to `true` only in the individual API function calls that need that security override:

```js
Parse.Cloud.define("like", function(request, response) {
Parse.Cloud.define("like", async request => {
var post = new Parse.Object("Post");
post.id = request.params.postId;
post.increment("likes");
post.save(null, { useMasterKey: true }).then(function() {
// If I choose to do something else here, it won't be using
// the master key and I'll be subject to ordinary security measures.
response.success();
}, function(error) {
response.error(error);
});
await post.save(null, { useMasterKey: true })
});
```

Expand Down
16 changes: 9 additions & 7 deletions _includes/common/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat
If you want to prevent restricted Sessions from modifying classes other than `User`, `Session`, or `Role`, you can write a Cloud Code `beforeSave` handler for that class:

```js
Parse.Cloud.beforeSave("MyClass", function(request, response) {
Parse.Session.current().then(function(session) {
if (session.get('restricted')) {
response.error('write operation not allowed');
}
response.success();
});
Parse.Cloud.beforeSave("MyClass", async request => {
const user = request.user;
const token = user.getSessionToken();
const query = new Parse.Query(Parse.Session);
query.equalTo('sessionToken', token);
const session = await q.first({ useMasterKey: true });
if (session.get('restricted')) {
throw 'write operation not allowed';
}
});
```
You can configure Class-Level Permissions (CLPs) for the Session class just like other classes on Parse. CLPs restrict reading/writing of sessions via the `Session` API, but do not restrict Parse Server's automatic session creation/deletion when users log in, sign up, and log out. We recommend that you disable all CLPs not needed by your app. Here are some common use cases for Session CLPs:
Expand Down
4 changes: 2 additions & 2 deletions _includes/embedded_c/cloud-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. See our
For example, you define a Cloud Function as below.

```cpp
Parse.Cloud.define("hello", function(request, response) {
response.success(request.body);
Parse.Cloud.define("hello", request => {
return request.body;
});
```

Expand Down
16 changes: 9 additions & 7 deletions _includes/rest/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat
If you want to prevent restricted Sessions from modifying classes other than `User`, `Session`, or `Role`, you can write a Cloud Code `beforeSave` handler for that class:

<pre><code class="javascript">
Parse.Cloud.beforeSave("MyClass", function(request, response) {
Parse.Session.current().then(function(session) {
if (session.get('restricted')) {
response.error('write operation not allowed');
}
response.success();
});
Parse.Cloud.beforeSave("MyClass", async request => {
const user = request.user;
const token = user.getSessionToken();
const query = new Parse.Query(Parse.Session);
query.equalTo('sessionToken', token);
const session = await q.first({ useMasterKey: true });
if (session.get('restricted')) {
throw 'write operation not allowed';
}
});
</code></pre>

Expand Down

0 comments on commit 02030f7

Please sign in to comment.