简体   繁体   English

ArangoDB-查询在FOXX中不起作用,但在Web界面中起作用

[英]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: 在AQL编辑器中的ArangoDB的Web界面中执行时,可以正常工作并返回预期的结果,但是当我尝试在FOXX存储库中执行时却给我一个错误:

'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: [ArangoError 3103:无法在[object Object] .Module上调用模块文件:c:/ Program Files / ArangoDB 2.6.2 / var / lib / arangodb-apps / _db / _system / exameFacil / APP / controllers / clinics.js]。 .run(C:\\ Program Files \\ ArangoDB 2.6.2 \\ bin ../ share / arangodb / js / common / bootstrap / modules.js:1420:20)在ArangoApp.loadAppScript(c:/ Program Files / ArangoDB 2.6。 2 / share / arangodb / js / server / modules / org / arangodb / foxx / arangoApp.js:452:24)在mountController(c:/ Program Files / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / foxx / routing.js:661:7)位于c:/ Program Files / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / foxx / routing.js:630:9 Object.routes(c上的routeApp(c:/ Program Files / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / foxx / routing.js:629:32)上的Array.forEach(本机) :/ Program Files / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / foxx / manager.js:268:10)位于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) 1054:74)在Object.routeRequest(c:/ Program Files)执行(c:/ Program Files / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / actions.js:1308:7) / ArangoDB 2.6.2 / share / arangodb / js / server / modules / org / arangodb / actions.js:1329:3),位于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分析错误。 JavaScript doesn't support multi-line strings as used in function searchProcedure . JavaScript不支持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). 要使查询字符串跨越多行,您将不得不使用字符串串联或模板字符串(用反引号括起来的字符串,ES6功能)。

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. 当处理用户生成的输入时,我建议还使用绑定参数将用户输入与实际查询字符串分开,并防止注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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