如何在Appsmith中向AQL查询传入集合名与文档键以更新文档
解决Appsmith连接ArangoDB时动态指定集合名的问题
问题场景
在Windows 10桌面环境下,使用Appsmith连接本地ArangoDB数据库时,尝试通过输入组件动态传入集合名称执行AQL查询失败,但将集合名硬编码到查询中可正常运行。试过@@、@、${}等多种绑定方式均未成功。
硬编码可正常运行的AQL代码
LET artiKey = "{{TestITartiKey.text}}" LET comSequ = 0 LET Comment = [ { "comSequ": 0, "comment": "{{InpITcomment.text}}" } ] FOR doc IN paeAnalysisAttributesNouns FILTER doc._key == artiKey UPDATE artiKey WITH { "comSequ": 0, Comments: UNION( doc.Comments, Comment ) } IN paeAnalysisAttributesNouns RETURN doc
失败的代码版本及错误信息
失败的AQL代码:
LET @@artiColVar = {{TestITartiColl.text}} LET artiKey = {{TestITartiKey.text}} LET artiCol = artiColVar LET comSequ = 0 LET Comment = [ { "comSequ": 0, "comment": "{{InpITcomment.text}}" } ] FOR doc IN @artiColVar FILTER doc._key == artiKey UPDATE artiKey WITH { "comSequ": 0, Comments: UNION( doc.Comments, Comment) IN @artiColVar } RETURN doc
错误信息:
Your query failed to execute: com.arangodb.ArangoDBException: Response: 400, Error: 1501 - AQL: syntax error, unexpected bind data source parameter, expecting identifier near '@@artiColVar = paeAnalysisAttrib...' at position 3:5 (while parsing) { "actionId":"65fc10a82f6b8a5a1dbb647d" "requestedAt":"2024-03-21 12:46:05" "requestParams":{ "Query":{ "value":"//Q2AB1FirstComment LET @@artiColVar = paeAnalysisAttributesNouns LET artiKey = 5095442 LET artiCol = artiColVar LET comSequ = 0 LET Comment = [ { \"comSequ\": 0, \"comment\": \"Results in two Account entries\" } ] FOR doc IN @artiCol FILTER doc._key == artiKey UPDATE artiKey WITH { \"comSequ\": 0, Comments: UNION( doc.Comments, Comment ) IN @artiCol } RETURN doc " } } }
期望实现:通过输入组件传入集合名,向指定集合的文档数组字段添加评论对象(硬编码时功能正常)。
错误原因分析
- AQL绑定规则错误:ArangoDB中动态指定集合名需使用
@@前缀的绑定变量,但不能通过LET语句赋值绑定变量,绑定变量需通过查询的参数传递,而非直接写在AQL语句中。 - 语法错误:失败代码中
UPDATE语句的括号位置错误,IN @artiColVar应放在WITH {...}的闭合括号之后。
正确解决方案
步骤1:修改AQL查询,使用正确的集合绑定方式
在AQL中用@@collection作为动态集合名的占位符,其余变量保持原有绑定方式:
LET artiKey = "{{TestITartiKey.text}}" LET comSequ = 0 LET Comment = [ { "comSequ": 0, "comment": "{{InpITcomment.text}}" } ] FOR doc IN @@collection FILTER doc._key == artiKey UPDATE artiKey WITH { "comSequ": 0, Comments: UNION( doc.Comments, Comment ) } IN @@collection RETURN doc
步骤2:在Appsmith中配置绑定参数
- 打开Appsmith中该ArangoDB查询的设置面板;
- 找到绑定参数(Bind Parameters)区域;
- 添加一个参数,键为
collection,值设置为{{TestITartiColl.text}}(即输入组件的集合名称值)。
关键说明
- ArangoDB中,
@@variable格式用于动态绑定集合/视图名称,而@variable用于绑定普通数据值; - 必须通过Appsmith的绑定参数传递集合名,而非直接将变量插入AQL字符串,这既符合AQL语法规则,也避免了注入风险;
- 确保输入组件
TestITartiColl的值是ArangoDB中存在的集合名称,且拼写完全一致(大小写敏感)。
验证
- 在输入组件中输入正确的集合名称;
- 执行查询,检查目标集合中的文档是否成功添加评论对象。
内容的提问来源于stack exchange,提问作者nilgai
相关产品推荐
相关产品推荐

