ArangoDB - 查询在 FOXX 中不起作用,但在 Web 界面中有效
ArangoDB - Query doesn't work in FOXX but works in web interface
这个查询:
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}
工作正常,return 在 ArangoDB 的 Web 界面中,在 AQL 编辑器中执行时的预期结果,但当我尝试在 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}'
}),
});
错误:
[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)
有什么建议吗?谢谢
错误的原因是示例代码中的 JavaScript 解析错误。 JavaScript 不支持函数 searchProcedure
中使用的多行字符串。要使查询字符串跨越多行,您将不得不使用字符串连接或模板字符串(用反引号括起来的字符串,ES6 特性)。
字符串连接示例:
searchProcedure: Foxx.createQuery({
query: 'FOR clinic IN exameFacil_clinics' +
' LET procedures_list = (' +
// ... string goes on here
'procedures_list: procedures_list}'
}),
使用模板字符串的示例:
searchProcedure: Foxx.createQuery({
query: `FOR clinic IN exameFacil_clinics
LET procedures_list = (
// ... string goes on here
procedures_list: procedures_list}`
}),
另一种方法是将查询字符串放在一行中。
对上述查询使用哪种替代方法是可读性和样式偏好的问题。
在处理用户生成的输入时,我建议还使用绑定参数将用户输入与实际查询字符串分开并防止注入。
这个查询:
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}
工作正常,return 在 ArangoDB 的 Web 界面中,在 AQL 编辑器中执行时的预期结果,但当我尝试在 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}'
}),
});
错误:
[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)
有什么建议吗?谢谢
错误的原因是示例代码中的 JavaScript 解析错误。 JavaScript 不支持函数 searchProcedure
中使用的多行字符串。要使查询字符串跨越多行,您将不得不使用字符串连接或模板字符串(用反引号括起来的字符串,ES6 特性)。
字符串连接示例:
searchProcedure: Foxx.createQuery({
query: 'FOR clinic IN exameFacil_clinics' +
' LET procedures_list = (' +
// ... string goes on here
'procedures_list: procedures_list}'
}),
使用模板字符串的示例:
searchProcedure: Foxx.createQuery({
query: `FOR clinic IN exameFacil_clinics
LET procedures_list = (
// ... string goes on here
procedures_list: procedures_list}`
}),
另一种方法是将查询字符串放在一行中。 对上述查询使用哪种替代方法是可读性和样式偏好的问题。
在处理用户生成的输入时,我建议还使用绑定参数将用户输入与实际查询字符串分开并防止注入。