Skip to content

Commit 02030f7

Browse files
authored
Replace response.success / response.error (parse-community#774)
1 parent e47a6e4 commit 02030f7

File tree

7 files changed

+36
-47
lines changed

7 files changed

+36
-47
lines changed

_includes/arduino/cloud-code.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. We provi
77
For example, you define a Cloud Function as below.
88

99
```javascript
10-
Parse.Cloud.define("hello", function(request, response) {
11-
response.success(request.body);
10+
Parse.Cloud.define("hello", request => {
11+
return request.body;
1212
});
1313
```
1414

_includes/common/errors.md

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ The following is a list of all the error codes that can be returned by the Parse
3232
| `ScriptFailed` | 141 | Cloud Code script failed. Usually points to a JavaScript error. Check error message for more details. |
3333
| `FunctionNotFound` | 141 | Cloud function not found. Check that the specified Cloud function is present in your Cloud Code script and has been deployed. |
3434
| `JobNotFound` | 141 | Background job not found. Check that the specified job is present in your Cloud Code script and has been deployed. |
35-
| `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. |
36-
| `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. |
3735
| `ValidationFailed` | 142 | Cloud Code validation failed. |
3836
| `WebhookError` | 143 | Webhook error. |
3937
| `InvalidImageData` | 150 | Invalid image data. |

_includes/common/performance.md

+9-14
Original file line numberDiff line numberDiff line change
@@ -1008,19 +1008,15 @@ You can use this to offload processing to the Parse servers thus increasing your
10081008
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:
10091009

10101010
```javascript
1011-
Parse.Cloud.define("averageStars", function(request, response) {
1012-
var Review = Parse.Object.extend("Review");
1013-
var query = new Parse.Query(Review);
1011+
Parse.Cloud.define("averageStars", async (request) => {
1012+
const query = new Parse.Query("Review");
10141013
query.equalTo("movie", request.params.movie);
1015-
query.find().then(function(results) {
1016-
var sum = 0;
1017-
for (var i = 0; i < results.length; ++i) {
1018-
sum += results[i].get("stars");
1019-
}
1020-
response.success(sum / results.length);
1021-
}, function(error) {
1022-
response.error("movie lookup failed");
1023-
});
1014+
const results = await query.find();
1015+
let sum = 0;
1016+
for (let i = 0; i < results.length; ++i) {
1017+
sum += results[i].get("stars");
1018+
}
1019+
return sum / results.length;
10241020
});
10251021
```
10261022

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

13091305
```javascript
13101306
var _ = require("underscore");
1311-
Parse.Cloud.beforeSave("Post", function(request, response) {
1307+
Parse.Cloud.beforeSave("Post", request => {
13121308
var post = request.object;
13131309
var toLowerCase = function(w) { return w.toLowerCase(); };
13141310
var words = post.get("text").split(/\b/);
@@ -1321,7 +1317,6 @@ Parse.Cloud.beforeSave("Post", function(request, response) {
13211317
hashtags = _.map(hashtags, toLowerCase);
13221318
post.set("words", words);
13231319
post.set("hashtags", hashtags);
1324-
response.success();
13251320
});
13261321
```
13271322

_includes/common/security.md

+5-13
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,10 @@ One particularly common use case for Cloud Code is preventing invalid data from
516516
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:
517517

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

550548
```js
551-
Parse.Cloud.define("like", function(request, response) {
549+
Parse.Cloud.define("like", async request => {
552550
var post = new Parse.Object("Post");
553551
post.id = request.params.postId;
554552
post.increment("likes");
555-
post.save(null, { useMasterKey: true }).then(function() {
556-
// If I choose to do something else here, it won't be using
557-
// the master key and I'll be subject to ordinary security measures.
558-
response.success();
559-
}, function(error) {
560-
response.error(error);
561-
});
553+
await post.save(null, { useMasterKey: true })
562554
});
563555
```
564556

_includes/common/sessions.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat
301301
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:
302302

303303
```js
304-
Parse.Cloud.beforeSave("MyClass", function(request, response) {
305-
Parse.Session.current().then(function(session) {
306-
if (session.get('restricted')) {
307-
response.error('write operation not allowed');
308-
}
309-
response.success();
310-
});
304+
Parse.Cloud.beforeSave("MyClass", async request => {
305+
const user = request.user;
306+
const token = user.getSessionToken();
307+
const query = new Parse.Query(Parse.Session);
308+
query.equalTo('sessionToken', token);
309+
const session = await q.first({ useMasterKey: true });
310+
if (session.get('restricted')) {
311+
throw 'write operation not allowed';
312+
}
311313
});
312314
```
313315
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:

_includes/embedded_c/cloud-code.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. See our
77
For example, you define a Cloud Function as below.
88

99
```cpp
10-
Parse.Cloud.define("hello", function(request, response) {
11-
response.success(request.body);
10+
Parse.Cloud.define("hello", request => {
11+
return request.body;
1212
});
1313
```
1414

_includes/rest/sessions.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat
286286
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:
287287

288288
<pre><code class="javascript">
289-
Parse.Cloud.beforeSave("MyClass", function(request, response) {
290-
Parse.Session.current().then(function(session) {
291-
if (session.get('restricted')) {
292-
response.error('write operation not allowed');
293-
}
294-
response.success();
295-
});
289+
Parse.Cloud.beforeSave("MyClass", async request => {
290+
const user = request.user;
291+
const token = user.getSessionToken();
292+
const query = new Parse.Query(Parse.Session);
293+
query.equalTo('sessionToken', token);
294+
const session = await q.first({ useMasterKey: true });
295+
if (session.get('restricted')) {
296+
throw 'write operation not allowed';
297+
}
296298
});
297299
</code></pre>
298300

0 commit comments

Comments
 (0)