Skip to main content
Skip table of contents

JavaScript API available in Templates

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:

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

Output:

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

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:

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

Output:

CODE
["id", "name"]

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:

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

‘Messy’ Output:

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

PocketQuery.queryArray()

Array<Object>

Returns the Query result as an array.

Example Template Code:

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

Output:

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

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.