简体   繁体   中英

ArangoDB - Query doesn't work in FOXX but works in web interface

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM