Inside your Templates, you can invoke JavaScript functions by making use of a <script>-tag. PocketQuery provides a global PocketQuery object that exposes some public API functions, making it easier to render your query results.

Charts

The PocketQuery.chart API can be used to render your Query results as charts. Check out its documentation here.

General

Name

Return Type

Description

PocketQuery.queryJson()

String

Returns the result of your Query in the form of JSON. This can be useful if you want to render the result of your Query completely yourself via JavaScript.

Example Template Code:

<script>
  var json = PocketQuery.queryJson();
  console.log(json);
</script>
JS

Output:

"[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Sarah\"}]"
CODE

PocketQuery.queryColumns()

Array<String>

Returns a list of string that represents the column headers in a table, parsed from the Query result.

Example Template Code:

<script>
  var json = PocketQuery.queryColumns();
  console.log(json);
</script>
CODE

Output:

["id", "name"]
CODE

If you have a messy result object that contains different columns in different rows, the result of PocketQuery.queryColumns will contain all row names in order of appearance. You can avoid this by using a Converter to clean up your result data.

‘Messy’ Result Data Example:

[
  {id: 1, name: "John"},
  {id: 2, age: 42}
]
CODE

‘Messy’ Output:

["id", "name", "age"]
CODE

PocketQuery.queryArray()

Array<Object>

Returns the Query result as an array.

Example Template Code:

<script>
  var array = PocketQuery.queryArray();
  console.log(array);
</script>
CODE

Output:

[{id: 1, name: "John"}, {id: 2, name: "Sarah"}]
CODE

On Server, the result of a Query always had to be an array. This changed in the Cloud version. Here, the result can be of any type. If your result is of any type other than array, PocketQuery.queryArray will return an array with the only element being your result object.