Parameters for Manual Operation

This topic explains function parameters when an endpoint in server code is manually called.

An endpoint is defined in the following way as shown in Server Code Syntax.

// Synchronous function
function funcName(params, context) {
  // Your code comes here...
}

// Asynchronous function
function funcName(params, context, done) {
  // Your code comes here...
}
  • The function name funcName becomes an endpoint name. You will use this endpoint name to specify the target function to run from the client side.
  • The first argument params is used to get execution parameters. The custom parameters you've passed from the client side will be stored here. For example, you can refer a value passed from the client as the parameter "username" with params.username.

    If you want to pass the information like the IP address where the server code was executed or which SDK was used for executing the server code, you need to pass them manually as the parameters.

  • The second argument context is used to get the application-related parameters.

    • Use the getAppID method to get the AppID.
    • Use the getAppKey method to get the AppKey.
    • Use the getAccessToken method to get the access token specified when the client requests the server code execution.
      • By executing the login with the access token, you can run the server code on behalf of this user (See the sample code in User who manually executes server code).
      • You can also use this method to determine if the server code is invoked by a logged-in user or by an anonymous user. The valid access token will be returned in the former case while null will be returned in the latter case.
    • Use getAppAdminContext method to get the app admin context (KiiAppAdminContext). You can execute user and group operations as an app admin with this context (See the sample code in Administrator).
  • The third argument done is a callback function for returning the result of the asynchronous server code to the client. See Asynchronous execution for more details.

If the server code is synchronous, the value specified in the return statement will be sent back to the client. If the server code is asynchronous, the value passed to the done (the third argument) will be sent back to the client.

If you want to return multiple values, consider using a JSON as shown in the next example. The client can get the JSON as is (for JavaScript SDK and REST) or as an instance of the JSONObject class (for other client SDKs).

function main(params, context, done) {
    done({"bonusScore" : 100, "bonusTime" : 30});
}

See Manually Executing Server Code to learn how to execute server code from the client.