Skip to main content

Return data

  • Each complete JavaScript code is equivalent to an anonymous function, and the returned data is equivalent to the return statement in the JavaScript function.

Just like traditional JavaScript code, after executing the [return data] statement, the entire function ends, and subsequent statements will not be executed again. After Luwak encounters returnResult() in JavaScript code, it assembles the data passed by returnResult into JSON and returns it to the caller.

Function definition

luwak.returnResult(number | string | array | object)

Parameters can be of multiple types without strong type verification.

Usage

Return a number

luwak.returnResult(1)

Corresponding response message:

{"jsonrpc": "2.0", "result": 1, "id": "client-unique-request-id"}

Return an object

luwak.returnResult({
"empNo": 10011,
"hireDate": "2023-1-1"
})

Corresponding response message:

{"jsonrpc": "2.0", "result": {"empNo": 10011, "hireDate": "2023-1-1"}, "id": "client-unique-request-id"}

Example of ending function execution

Query the employees whose joining date is 1999-05-01 among the three employees 10003, 10004, and 10005.

Note: If you just execute luwak.runSql, the caller cannot get the data. It only gets it in the js code and does not return it. At this time, you need to call the luwak.returnResult function to return.

JavaScript code

var params = {
hireDate: "1999-05-01"
}

var config = {
sql: "select * from hrm.`employees` where emp_no in ('10003','10004','10005') and hire_date = :hireDate;"
}

result = luwak.runSql(config, params)

// if (result) { Process the query results }

//return results
luwak.returnResult(result)

log.info("log after returnResult");

luwak log

Open the log/luwak-info.log file and find that the statement log after returnResult is not printed, which shows that it has not been executed.

2023/10/25 18:08:50 DEBUG dao_util.go:50 select * from hrm.`employees` where emp_no in ( '10003','10004','10005') and hire_date = ? ; - ["20" ] - 26.801733ms
2023/10/25 18:08:50 DEBUG server.go:246 127.0.0.1 | HTTP/1.1 | POST /console/js_code - 200 - 34.464409ms | Apache-HttpClient/4.5.13 (Java/11.0.11)