Cloud Code Parameters Reference

Cloud Code Parameters Reference

When writing business logic using Cloud Code for Data or Cloud Functions, a number of parameters to the data event handlers or the Cloud functions give you direct access to useful information. You need it to apply more complex and robust business logic to your app.

In this article:

Parameters

The following table lists all parameters available when working with Cloud Code for Data event handlers or Cloud Functions. Note that some parameters are available only in certain event handlers.

Name Type Availability Description
request Object All An object representing the incoming request to the server.
response Object Cloud Code for Data after events An object representing the response returned by the server.
context Object Cloud Code for Data An object that can be used to send data from before to after events.
done Function All A function you need to call to complete the code execution.

Request Object Fields

The request object gives you access to all data sent with a request such as HTTP verb, data, and custom parameters.

The following table lists fields of the request object common to all requests.

Name Type Description
action String The HTTP method used for the request. Can be GET, PUT, POST, or DELETE.
typeName String The name of the content type or Cloud Function the request is made to.
typeId GUID or null The system ID of the content type or Cloud Function the request is made to.
itemId String The ID of the item if the request is made by ID. The value is null if the request fetches all items or uses a filter.
customParameters Object An object representing the parsed X-Everlive-Custom-Parameters header.
options Object System field. Does not provide data beneficial to the developer.
queryString Object An object representing any parameters included with the query string.
data Object or null An object representing the request body. The value is null for HTTP methods that do not support body.
principal Object Information about the user account making the request.

The following table lists fields of the request object applicable only to Cloud Code for Data.

Name Type Description
sortExpression Object An object representing the sorting expression sent with the request.
fieldsExpression Object An object representing the fields projection expression sent with the request.
skip Number When paging, a value representing the number of items to skip sent with the request.
take Number When paging, a value representing the page size sent with the request.
aggregateExpression Object An object representing the aggregation expression sent with the request.
isSync Boolean A boolean flag indicating whether the request is initiated by offline synchronization.

request.principal

The principal field of the request object provides information about the user making the request.

Name Type Description
type String The type of the user account impersonating the request. Can be user, anonymous, or tfis.
id GUID The ID of the user account making the request. It is an empty GUID if type is not "user".
displayName String The DisplayName field value of the user account making the request.
data Object or null The user account object of the user making the request. Has a null value if the request is made anonymously.
identityProvider String The name of the identity provider that authenticates the user. Examples: Everlive, Facebook, Google, Twitter, ADFS, SAML.

Response Object Fields

The response object gives you access to information such as the HTTP status code, the returned data, and so on.

The following table lists all possible fields of the response object.

Name Type Description
statusCode Number The HTTP status code of the request.
result Array or Object The object returned by the server. For example, the result of a READ operation.
error Object or null An object containing error data if the operation resulted in an error or null if the request completed successfully.
additionalData Object An object which you can use to return additional data to the client.

Context Object Fields

When running Cloud Code for Data, the context object can be used to pass literals from before to after event handlers of the same type: beforeCreate to afterCreate, beforeRead to afterRead, and so on.

Example:

Everlive.Events.beforeCreate(function(request, context, done) {
    context.fromBeforeCreate = "A message from beforeCreate";
    done();
});


Everlive.Events.afterCreate(function(request, response, context, done) {
    console.log(context.fromBeforeCreate); // "A message from beforeCreate"
    done();
});

Ending Execution

The done parameter is the name of a callback function which you need to call to end code execution. After that, normal request processing continues. Invoke done everywhere you consider the business logic work to be done.

Everlive.Events.beforeRead(function(request, context, done) {
    if (!request.itemId) {
        Everlive.Response.setErrorResult("Please specify an Id and try again.", 100101, 403);
        done();
    } else {
        done();
    }
});    

Because many Business Logic operations are asynchronous, you may need to call done in several places where you have to await the operation result. For example, when using multiple asynchronous calls, call done at each end of the control flow to prevent timeouts of the business logic.

var contacts = Everlive.Sdk.fromContext().data('Contacts');

var existingContactsFilter = new Everlive.Sdk.Query();
existingContactsFilter.where().isin('ContactName', ['contactName1', 'contactName2']);

contacts.get(existingContactsFilter).then(function(data) {
    // do something with the data here

    done();
}, function(error) {
    // handle the error here

    done();
});

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.