Outputs
You return various data output types from your cloud modules to send data to the client.
Return
return some_value[, type_constant]
Example
... function api.post.cats() return "Meow!" end
Types
Did You Know?
All "outputs" are converted to JSON before being sent down the wire. Any and all data must be contained in a result
key. This process is handled automatically if you follow the return
protocol.
String
... function api.post.print_string() return "Here is some text" end --Produces: {"result":"Here is some text"}
Number
... function api.post.print_number() return 25 end --Produces: {"result":25}
Boolean
... function api.post.print_boolean() return false end --Produces: {"result":false}
Array
... function api.post.print_array() return { 'Orange', 'Apple', 'Grapes' } end --Produces: {"result":["Orange", "Apple", "Grapes"]}
Table
... function api.post.print_table() return { user = "Tina" } end --Produces: {"result":{"user":"Tina"}}
Error
cloud.error
To use a structured error
return, you can wrap the error results with this method.
Signature
cloud.error( error_str[, error_code] )
Parameters
Name | Description | Type | Default | Required |
---|---|---|---|---|
error_str |
A descriptive error string. | string | nil |
Y |
error_code |
A numerical error code. | number | nil |
N |
Example
... function api.post.dogs() --whoops, wet couch... return cloud.error("Woof!", 911) end --Produces: {"result":{"error":"Woof!", "errorCode":911}}
Custom
You can "customize" the result table key for use case specific needs.
You Should Know
Customized result tables are not compatible with the Corona SDK module unless you use the same result
and error
key data package schema.
To customize a result
key, you can do the following:
... function api.post.new_result() return cloud.result( result, 'new_result_key' ) --Produces: {"new_result_key":<JSONEncodedResult>} end