node-client-api
node-client-api copied to clipboard
DatabaseClient.eval() always return true when the result is a boolean
When evaluating a piece of JavaScript on the server using DatabaseClient.eval()
seems to always return true
when the expression results in a single boolean. Even when the result is false
.
To reproduce:
require('marklogic')
.createDatabaseClient({
host: 'localhost',
port: '8000',
database: 'neo-elec-content',
user: 'admin',
password: 'admin',
authType: 'DIGEST'
})
.eval(`false;`)
.result()
.then(res => console.dir(res));
returns the following:
[ { format: 'text', datatype: 'boolean', value: true } ]
We would expect of course value
to be false
. If you change the expression in the eval()
to, say, '42;'
, then you get the correct result:
[ { format: 'text', datatype: 'integer', value: 42 } ]
I use the marklogic
package version 2.9.0 (installed last week with npm i marklogic
.)
Looks like this is where that response object is generated for boolean
:
https://github.com/marklogic/node-client-api/blob/master/lib/server-exec.js#L75
and Boolean(data.content)would produce the correct boolean value if content is false
let data = {"content": false};
Boolean(data.content);
So, the issue is further upstream in how the data.content
is produced from the results, and that looks like it is done in the marshal()
function
https://github.com/marklogic/node-client-api/blob/ee49df1fa05bb29908f931b2f13bce69470c2567/lib/mlutil.js#L215
where boolean
falls through to the return String(data)
at the end.
I think it needs an extra if statement to handle boolean:
else if (typeof data == "boolean") {
return data;
}
If that is the case, when making a fix for boolean
, should ensure that we have full coverage for all primitive types (and figure out if/when we will be upgrading the MarkLogic V8 engine and can handle bigint )
This test to verify boolean only verifies true
, which is why it was not discovered. It would be helpful to have tests for both true
and false
https://github.com/marklogic/node-client-api/blob/master/test-basic/server-exec.js#L78
The fix for this issue will be available in the upcoming release.
Closing this since fix is now available in version 3.5.0. Thanks.!