Does anyone have JavaScript Server Side Functions working?

480 views
Skip to first unread message

MV-dev1

unread,
Sep 9, 2015, 6:31:40 PM9/9/15
to orient-...@googlegroups.com
I can't call server side functions (from NodeJS/OrientJs) that use the 'orient' object on the server to access the graph database.

>> Does anyone have this working in 2.1.1?

I went ahead and wrote this bug but hope that I'm wrong because this is blocking me from releasing my first OrientDb implementation.  https://github.com/orientechnologies/orientdb/issues/4947

Here's an example of a server function:

var g = orient.getGraph();
var results = g.command("sql", "SELECT FROM OUser");
return results;


Here's an example of some calling code from NodeJs using OrientJs...

var OrientDB = require('orientjs');
var server = OrientDB({host: 'localhost', port: 2424, username: 'admin', password: 'admin'});
var db = server.use({name: 'TestDb', username: 'admin', password: 'admin'});
db
.exec("SELECT testfunction()", {params: {}}) .then(function (results) { console.log("Results = " + JSON.stringify(results)); .......

I see these errors in the log...

2015-09-10 00:33:47:730 INFO  OrientDB Server v2.1.1 (build UNKNOWN@r${buildNumber}; 2015-08-31 18:08:38+0000) is active. [OServer]{db=TestDb} Error on unmarshal
ling record #-2:1 (java.lang.ClassCastException: com.tinkerpop.blueprints.impls.orient.OrientVertex cannot be cast to com.orientechnologies.orient.core.record.O
Record)
java.lang.ClassCastException: com.tinkerpop.blueprints.impls.orient.OrientVertex cannot be cast to com.orientechnologies.orient.core.record.ORecord
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerStringAbstract.fieldTypeToString(ORecordSerializerStringAbs
tract.java:197)
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerCSVAbstract.embeddedCollectionToStream(ORecordSerializerCSV
Abstract.java:843)
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerCSVAbstract.fieldToStream(ORecordSerializerCSVAbstract.java
:534)
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerSchemaAware2CSV.toString(ORecordSerializerSchemaAware2CSV.j
ava:506)
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerStringAbstract.toStream(ORecordSerializerStringAbstract.jav
a:689)
        at com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerSchemaAware2CSV.toStream(ORecordSerializerSchemaAware2CSV.j
ava:268)
        at com.orientechnologies.orient.core.record.impl.ODocument.toStream(ODocument.java:2282)
        at com.orientechnologies.orient.core.record.impl.ODocument.toStream(ODocument.java:671)
        at com.orientechnologies.orient.server.network.protocol.binary.OBinaryNetworkProtocolAbstract.getRecordBytes(OBinaryNetworkProtocolAbstract.java:424)
        at com.orientechnologies.orient.server.network.protocol.binary.OBinaryNetworkProtocolAbstract.writeRecord(OBinaryNetworkProtocolAbstract.java:443)
        at com.orientechnologies.orient.server.network.protocol.binary.OBinaryNetworkProtocolAbstract.writeIdentifiable(OBinaryNetworkProtocolAbstract.java:141)
        at com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary.serializeValue(ONetworkProtocolBinary.java:1389)
        at com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary.command(ONetworkProtocolBinary.java:1337)
        at com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary.executeRequest(ONetworkProtocolBinary.java:396)
        at com.orientechnologies.orient.server.network.protocol.binary.OBinaryNetworkProtocolAbstract.execute(OBinaryNetworkProtocolAbstract.java:223)
        at com.orientechnologies.common.thread.OSoftThread.run(OSoftThread.java:77)

2015-09-10 00:34:14:152 WARNI {db=TestDb} Cannot serialize record: #-2:1{testfunction:[4]} v0 [ONetworkProtocolBinary]

David Carr

unread,
Sep 10, 2015, 1:04:28 PM9/10/15
to OrientDB
I use orientjs and javascript server-side functions extensively on 2.1.1. Not sure which part of this is causing your issue but my server-side functions generally follow this pattern:

var db = orient.getDatabase();
var cmd = 'select from #19:1';
var result = db.command(cmd);
return result;

I'd recommend running your server-side function via orient's web front-end

http://<host>:2480/studio/index.html#/database/<db_name>/functions 

to make sure they are performing properly and then if you are still having problems you'll know the problem is on the orienjs side. Feel free to post back if I can help more.

MV-dev1

unread,
Sep 10, 2015, 4:29:46 PM9/10/15
to orient-...@googlegroups.com
Thanks for the reply.

I did try what you suggested with my basic example using var db = orient.getDatabase() instead of var db = orient.getGraph() and it now works (doesn't crash) on both the server and via NodeJs.  Not sure where I saw getGraph() but it was running in Studio so I didn't give that a second thought.

But the data isn't as I'd expect.

In Studio/Functions I see everything expanded but from Node/ReactJs I only get the ids...   Any ideas how to expand that before it's sent down?

[
   
{
       
"@type": "d",
       
"@rid": "#5:0",
       
"@version": 2,
       
"@class": "OUser",
       
"name": "admin",
       
"password": "{SHA-256}AA6C00A14B7C8EC02F1849F51DC96EA55504E1F55E793C6840007848B6724EC2",
       
"status": "ACTIVE",
       
"roles": [
           
"#4:0"
       
],
       
"@fieldTypes": "roles=n"
   
},
   
{
       
"@type": "d",
       
"@rid": "#5:1",
       
"@version": 1,
       
"@class": "OUser",
       
"name": "reader",
       
"password": "{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30",
       
"status": "ACTIVE",
       
"roles": [
           
"#4:1"
       
],
       
"@fieldTypes": "roles=n"
   
},...

JSON in JavaScript result...

[{"@type":"d","testfunction2":["#5:0","#5:1","#5:2"],"@rid":"#-2:1"}]

Oh, it's not expanded when I call it from the Browse tab of Orient Studio either...

SELECT testfunction2()

 #5:0    #5:1 #5:2 


My server side function is now as follows:

//var g = orient.getGraph();
//var results = g.command("sql", "SELECT FROM OUser");
//return results;


var db = orient.getDatabase();
var cmd = 'SELECT * FROM OUser';

var result = db.command(cmd);
return result;



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

PS  Maybe the bug is with orient.getGraph() -- it's in every example that I see..

From http://orientdb.com/docs/2.0/orientdb.wiki/Functions.html

Access to the databases from Functions

OrientDB always binds a special variable "orient" to use OrientDB services from inside the functions. The most important methods are:


and...

http://stackoverflow.com/questions/27657784/how-to-extract-the-result-of-a-query-from-json-inside-a-custom-javascript-functi
http://pizzaconnections.net/2015/04/02/orientdb-functions-by-samples-1/
https://groups.google.com/forum/#!topic/orient-database/3ByJk559flw
https://github.com/orientechnologies/orientdb/issues/4255
https://github.com/orientechnologies/orientdb/issues/4053

MV-dev1

unread,
Sep 10, 2015, 5:01:51 PM9/10/15
to OrientDB
By the way...   I did add

SELECT @this.toJSON() FROM OUser;

and found another bug...

It's creating an infinite sized JSON object.

Trying to call JSON.parse() on the results gives...
"C:\Program Files (x86)\JetBrains\WebStorm 9.0.1\bin\runnerw.exe" "c:\program files\nodejs\node.exe" testo2.js
Unhandled rejection TypeError: Converting circular structure to JSON
    at
Object.stringify (native)
    at
Db.<anonymous> (d:\Test\src\OTests\node_modules\testo2.js:14:26)
    at
Db.tryCatcher (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\util.js:26:23)
    at
Promise._settlePromiseFromHandler (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\promise.js:503:31)
    at
Promise._settlePromiseAt (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\promise.js:577:18)
    at
Promise._settlePromises (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\promise.js:693:14)
    at
Async._drainQueue (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\async.js:123:16)
    at
Async._drainQueues (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\async.js:133:10)
    at
Immediate.Async.drainQueues [as _onImmediate] (d:\Test\src\OTests\node_modules\orientjs\node_modules\bluebird\js\main\async.js:15:14)
    at processImmediate
[as _immediateCallback] (timers.js:358:17)

Reply all
Reply to author
Forward
0 new messages