driven85 driven85 - 1 year ago 62
JSON Question

How to extract the result of a query from JSON inside a custom javascript function in OrientDB

I created a custom javascript function in the OrientDB Studio. The function takes a rid as an argument and performs a select query from that rid.

var graph = orient.getGraph();
var res = graph.command( "sql", "select outE('Rel').inV('B').size() as lower_num from " + rid );

So, the result of this query is a number. Depending on the value of this number the function should do different things. I wonder how to extract and use that value from the res variable inside the function. All my attempts have failed so far.. If I return the res variable from the function, the result looks like this:

"@type": "d",
"@rid": "#-2:1",
"@version": 0,
"lower_num": 2

So, I tried the following
( res[0]['lower_num'] == 2 ); ( res['lower_num'] == 2 )
and nothing worked.. They always returned false. Any ideas?

Answer Source

OrientDB put ODocument objects in the result set array. So try this:

return res[0].getRecord().field( "lower_num" );