Cloud

Parse. Cloud

new Cloud()

Contains functions for calling and declaring cloud functions.

Some functions are only available from Cloud Code.

Methods

(static) afterDelete(arg1, func)

Registers an after delete function.

Available in Cloud Code only.

If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should pass the class itself and not the String for arg1.

Parse.Cloud.afterDelete('MyCustomClass', (request) => {
  // code here
})

Parse.Cloud.afterDelete(Parse.User, (request) => {
  // code here
})
Parameters:
Name Type Description
arg1 string | Parse.Object

The Parse.Object subclass to register the after delete function for. This can instead be a String that is the className of the subclass.

func function

The function to run after a delete. This function should take just one parameter, Parse.Cloud.TriggerRequest.

(static) afterSave(arg1, func)

Registers an after save function.

Available in Cloud Code only.

If you want to use afterSave for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should pass the class itself and not the String for arg1.

Parse.Cloud.afterSave('MyCustomClass', function(request) {
  // code here
})

Parse.Cloud.afterSave(Parse.User, function(request) {
  // code here
})
Parameters:
Name Type Description
arg1 string | Parse.Object

The Parse.Object subclass to register the after save function for. This can instead be a String that is the className of the subclass.

func function

The function to run after a save. This function should take just one parameter, Parse.Cloud.TriggerRequest.

(static) afterSaveFile(func)

Registers an after save file function.

Available in Cloud Code only.

Example: creating a new object that references this file in a separate collection

Parse.Cloud.afterSaveFile(async ({ file, user }) => {
  const fileObject = new Parse.Object('FileObject');
  fileObject.set('metadata', file.metadata());
  fileObject.set('tags', file.tags());
  fileObject.set('name', file.name());
  fileObject.set('createdBy', user);
  await fileObject.save({ sessionToken: user.getSessionToken() });
});
Parameters:
Name Type Description
func function

The function to run after a file saves. This function should take one parameter, a Parse.Cloud.FileTriggerRequest.

(static) beforeConnect(func)

Parameters:
Name Type Description
func function

The function to before connection is made. This function can be async and should take just one parameter, Parse.Cloud.ConnectTriggerRequest.

(static) beforeDelete(arg1, func)

Registers an before delete function.

Available in Cloud Code only.

If you want to use beforeDelete for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should pass the class itself and not the String for arg1.

Parse.Cloud.beforeDelete('MyCustomClass', (request) => {
  // code here
})

Parse.Cloud.beforeDelete(Parse.User, (request) => {
  // code here
})
Parameters:
Name Type Description
arg1 string | Parse.Object

The Parse.Object subclass to register the before delete function for. This can instead be a String that is the className of the subclass.

func function

The function to run before a delete. This function should take just one parameter, Parse.Cloud.TriggerRequest.

(static) beforeSave(arg1, func)

Registers an before save function.

Available in Cloud Code only.

If you want to use beforeSave for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should pass the class itself and not the String for arg1.

Parse.Cloud.beforeSave('MyCustomClass', (request) => {
  // code here
})

Parse.Cloud.beforeSave(Parse.User, (request) => {
  // code here
})
Parameters:
Name Type Description
arg1 string | Parse.Object

The Parse.Object subclass to register the after save function for. This can instead be a String that is the className of the subclass.

func function

The function to run before a save. This function should take just one parameter, Parse.Cloud.TriggerRequest.

(static) beforeSaveFile(func)

Registers an before save file function. A new Parse.File can be returned to override the file that gets saved. If you want to replace the rquesting Parse.File with a Parse.File that is already saved, simply return the already saved Parse.File. You can also add metadata to the file that will be stored via whatever file storage solution you're using.

Available in Cloud Code only.

Example: Adding metadata and tags

Parse.Cloud.beforeSaveFile(({ file, user }) => {
  file.addMetadata('foo', 'bar');
  file.addTag('createdBy', user.id);
});

Example: replacing file with an already saved file

Parse.Cloud.beforeSaveFile(({ file, user }) => {
  return user.get('avatar');
});

Example: replacing file with a different file

Parse.Cloud.beforeSaveFile(({ file, user }) => {
  const metadata = { foo: 'bar' };
  const tags = { createdBy: user.id };
  const newFile = new Parse.File(file.name(), <some other file data>, 'text/plain', metadata, tags);
  return newFile;
});

Parameters:
Name Type Description
func function

The function to run before a file saves. This function should take one parameter, a Parse.Cloud.FileTriggerRequest.

(static) beforeSubscribe(arg1, func)

Parameters:
Name Type Description
arg1 string | Parse.Object

The Parse.Object subclass to register the before subscription function for. This can instead be a String that is the className of the subclass.

func function

The function to run before a subscription. This function can be async and should take one parameter, a Parse.Cloud.TriggerRequest.

(static) define(name, data)

Defines a Cloud Function.

Available in Cloud Code only.

Parameters:
Name Type Description
name string

The name of the Cloud Function

data function

The Cloud Function to register. This function should take one parameter Parse.Cloud.FunctionRequest

(static) getJobsData() → {Promise}

Gets data for the current set of cloud jobs.

Returns:
Type:
Promise

A promise that will be resolved with the result of the function.

(static) getJobStatus(jobStatusId) → {Parse.Object}

Gets job status by Id

Parameters:
Name Type Description
jobStatusId string

The Id of Job Status.

Returns:
Type:
Parse.Object

Status of Job.

(static) httpRequest(options) → {Promise.<Parse.Cloud.HTTPResponse>}

Makes an HTTP Request.

Available in Cloud Code only.

By default, Parse.Cloud.httpRequest does not follow redirects caused by HTTP 3xx response codes. You can use the followRedirects option in the Parse.Cloud.HTTPOptions object to change this behavior.

Sample request:

Parse.Cloud.httpRequest({
  url: 'http://www.example.com/'
}).then(function(httpResponse) {
  // success
  console.log(httpResponse.text);
},function(httpResponse) {
  // error
  console.error('Request failed with response code ' + httpResponse.status);
});
Parameters:
Name Type Description
options Parse.Cloud.HTTPOptions

The Parse.Cloud.HTTPOptions object that makes the request.

Returns:
Type:
Promise.<Parse.Cloud.HTTPResponse>

A promise that will be resolved with a Parse.Cloud.HTTPResponse object when the request completes.

(static) job(name, func)

Defines a Background Job.

Available in Cloud Code only.

Parameters:
Name Type Description
name string

The name of the Background Job

func function

The Background Job to register. This function should take two parameters a Parse.Cloud.JobRequest and a Parse.Cloud.JobStatus

(static) run(name, data, options) → {Promise}

Makes a call to a cloud function.

Parameters:
Name Type Description
name string

The function name.

data object

The parameters to send to the cloud function.

options object
Returns:
Type:
Promise

A promise that will be resolved with the result of the function.

(static) startJob(name, data) → {Promise}

Starts a given cloud job, which will process asynchronously.

Parameters:
Name Type Description
name string

The function name.

data object

The parameters to send to the cloud function.

Returns:
Type:
Promise

A promise that will be resolved with the jobStatusId of the job.

Type Definitions

ConnectTriggerRequest

Properties:
Name Type Description
installationId string

If set, the installationId triggering the request.

useMasterKey boolean

If true, means the master key was used.

user Parse.User

If set, the user that made the request.

clients number

The number of clients connected.

subscriptions number

The number of subscriptions connected.

sessionToken string

If set, the session of the user that made the request.

FileTriggerRequest

Properties:
Name Type Description
installationId string

If set, the installationId triggering the request.

master boolean

If true, means the master key was used.

user Parse.User

If set, the user that made the request.

file Parse.File

The file triggering the hook.

ip string

The IP address of the client making the request.

headers object

The original HTTP headers for the request.

triggerName string

The name of the trigger (beforeSaveFile, afterSaveFile, ...)

log object

The current logger inside Parse Server.

FunctionRequest

Properties:
Name Type Description
installationId string

If set, the installationId triggering the request.

master boolean

If true, means the master key was used.

user Parse.User

If set, the user that made the request.

params object

The params passed to the cloud function.

HTTPOptions

Properties:
Name Type Description
body string | object

The body of the request. If it is a JSON object, then the Content-Type set in the headers must be application/x-www-form-urlencoded or application/json. You can also set this to a Buffer object to send raw bytes. If you use a Buffer, you should also set the Content-Type header explicitly to describe what these bytes represent.

error function

The function that is called when the request fails. It will be passed a Parse.Cloud.HTTPResponse object.

followRedirects boolean

Whether to follow redirects caused by HTTP 3xx responses. Defaults to false.

headers object

The headers for the request.

method string

The method of the request. GET, POST, PUT, DELETE, HEAD, and OPTIONS are supported. Will default to GET if not specified.

params string | object

The query portion of the url. You can pass a JSON object of key value pairs like params: {q : 'Sean Plott'} or a raw string like params:q=Sean Plott.

success function

The function that is called when the request successfully completes. It will be passed a Parse.Cloud.HTTPResponse object.

url string

The url to send the request to.

HTTPResponse

Properties:
Name Type Description
buffer Buffer

The raw byte representation of the response body. Use this to receive binary data. See Buffer for more details.

cookies object

The cookies sent by the server. The keys in this object are the names of the cookies. The values are Parse.Cloud.Cookie objects.

data object

The parsed response body as a JavaScript object. This is only available when the response Content-Type is application/x-www-form-urlencoded or application/json.

headers object

The headers sent by the server. The keys in this object are the names of the headers. We do not support multiple response headers with the same name. In the common case of Set-Cookie headers, please use the cookies field instead.

status number

The status code.

text string

The raw text representation of the response body.

JobRequest

Properties:
Name Type Description
params object

The params passed to the background job.

JobStatus

Properties:
Name Type Description
error function

If error is called, will end the job unsuccessfully with an optional completion message to be stored in the job status.

message function

If message is called with a string argument, will update the current message to be stored in the job status.

success function

If success is called, will end the job successfullly with the optional completion message to be stored in the job status.

TriggerRequest

Properties:
Name Type Description
installationId string

If set, the installationId triggering the request.

master boolean

If true, means the master key was used.

user Parse.User

If set, the user that made the request.

object Parse.Object

The object triggering the hook.

ip string

The IP address of the client making the request.

headers object

The original HTTP headers for the request.

triggerName string

The name of the trigger (beforeSave, afterSave, ...)

log object

The current logger inside Parse Server.

original Parse.Object

If set, the object, as currently stored.