Return statement not working with User defined functions in Arangodb? How to write an AQl query inside a function?

Viewed 19
require("@arangodb/aql/functions").register(
    "MYFUNCTIONS::VERTEX::INDEGREE", 
    function(vertex,edge, node) {
        "use strict"; 
        AQL_Query(
            (return( "for t in Transaction  collect vertex_count=t._from with into n return x"))
        )
    }
);

throws the following exception

JavaScript exception: SyntaxError: Unexpected token 'return'
!require("@arangodb/aql/functions").register("MYFUNCTIONS::VERTEX::INDEGREE", function(vertex,edge, node) {"use strict"; AQL_Query((return( "for t in Transaction  collect vertex_count=t._from with into n return x")))});
!                                                                                                                                   ^^^^^^
stacktrace: SyntaxError: Unexpected token 'return'
1 Answers

Use the aql template string handler to assamble the query, something like this should get you started:

require("@arangodb/aql/functions").register(
    "MYFUNCTIONS::VERTEX::INDEGREE", 
    function(vertex,edge, node) {
        "use strict";
        let db = require('@arangodb').db;
        let aql = require('@arangodb').aql;
        
        let query = aql`
            for t in Transaction  
              collect vertex_count=t._from with into n 
              return n
        `;
        return db._query(query).toArray();
    }
);
Related