0

This query:

FOR clinic IN exameFacil_clinics
                    LET procedures_list = (
                    FOR procedure IN clinic.procedures
                    FILTER LIKE(procedure.name, "%hemo%", true)
                    COLLECT procedures_list = procedure.name
                    RETURN procedures_list
                )
                FILTER LENGTH(procedures_list) > 0
                RETURN{
                        clinic_name: clinic.name,
                        procedures_list: procedures_list}

works fine and return the expected result when executing in the web interface for ArangoDB, in the AQL editor, but gives me an error when I try to execute in a FOXX repository:

'use strict';
var Foxx = require('org/arangodb/foxx');

module.exports = Foxx.Repository.extend({
  // Add your custom methods here

  //Return all procedures from a clinic, given the clinic id
  getAllProcedures: Foxx.createQuery({
        query: 'FOR clinic IN exameFacil_clinics FILTER clinic._key == @id RETURN clinic.procedures',
        params: ['id']

    }),

  //Make a 'LIKE' query in all procedures from all clinics, given the search string ( procedure name )
  searchProcedure: Foxx.createQuery({
        query: 'FOR clinic IN exameFacil_clinics
                    LET procedures_list = (
                    FOR procedure IN clinic.procedures
                    FILTER LIKE(procedure.name, "%hemo%", true)
                    COLLECT procedures_list = procedure.name
                    RETURN procedures_list
                )
                FILTER LENGTH(procedures_list) > 0
                RETURN{
                        clinic_name: clinic.name,
                        procedures_list: procedures_list}'

    }),

});

The error:

[ArangoError 3103: failed to invoke module File: c:/Program Files/ArangoDB 2.6.2/var/lib/arangodb-apps/_db/_system/exameFacil/APP/controllers/clinics.js] at [object Object].Module.run (C:\Program Files\ArangoDB 2.6.2\bin../share/arangodb/js/common/bootstrap/modules.js:1420:20) at ArangoApp.loadAppScript (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/foxx/arangoApp.js:452:24) at mountController (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/foxx/routing.js:661:7) at c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/foxx/routing.js:630:9 at Array.forEach (native) at routeApp (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/foxx/routing.js:629:32) at Object.routes (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/foxx/manager.js:268:10) at foxxRouting (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/actions.js:1054:74) at execute (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/actions.js:1308:7) at Object.routeRequest (c:/Program Files/ArangoDB 2.6.2/share/arangodb/js/server/modules/org/arangodb/actions.js:1329:3) at Function.actions.defineHttp.callback (c:\Program Files\ArangoDB 2.6.2\share\arangodb\js\actions\api-system.js:58:15)

Any advice? Thanks

Pavarine
  • 637
  • 3
  • 15
  • 30
  • I think the code above doesn't parse. JavaScript doesn't support multi-line strings as you've used one in function `searchProcedure`. If you want the query string to span multiple lines, you will either have to use string concatenation or a template string (a string enclosed in backticks, ES6 feature). Another alternative is to put the query string onto a single line. – stj Aug 03 '15 at 06:41
  • You're totally right, thanks. If you create a answer I accept it. – Pavarine Aug 03 '15 at 17:01
  • btw: next release version will have much better error reporting for this, pointing at the actual error line in the "offending" script, and not only showing a backtrace for the Foxx internal function that calls it. – stj Aug 04 '15 at 07:02
  • The ArangoDb devs are always ready to help the community, this makes me persist on this..thanks – Pavarine Aug 05 '15 at 23:15

1 Answers1

2

The reason for the error is a JavaScript parse error in the example code. JavaScript doesn't support multi-line strings as used in function searchProcedure. To make a query string span multiple lines, you will either have to use string concatenation or a template string (a string enclosed in backticks, ES6 feature).

Example for string concatenation:

searchProcedure: Foxx.createQuery({
  query: 'FOR clinic IN exameFacil_clinics' + 
         '  LET procedures_list = (' +
         // ... string goes on here 
         'procedures_list: procedures_list}'
}),

Example for using a template string:

searchProcedure: Foxx.createQuery({
  query: `FOR clinic IN exameFacil_clinics
            LET procedures_list = (
          // ... string goes on here 
          procedures_list: procedures_list}`
}),

Another alternative is to put the query string onto a single line. Which alternative to use for the above query is a matter of readability and style preferences.

When dealing with user-generated input, I suggest also using bind parameters to separate user input from the actual query string and protect against injections.

stj
  • 9,037
  • 19
  • 33