获取“ArangoError:绑定参数'value1'未在查询中声明(解析时)”

时间:2016-04-17 18:56:48

标签: arangodb foxx

我的Foxx应用程序中有这样的代码:

db._query(aqlQuery`
  FOR d IN FULLTEXT('api_texts', '${args.attribute}', '${args.search}')
    SORT d.${args.sort_by} ${args.sort}
    LIMIT ${args.skip}, ${args.limit}
    RETURN d
`).toArray()

这似乎是正确的,但由于某种原因,它给了我一个错误bind parameter 'value1' was not declared in the query (while parsing)

我试图在arangosh中运行它...同样的错误:

arangosh [ilearn]> db._query({ "query" : "\n        FOR d IN FULLTEXT('api_texts', '@value0', '@value1')\n          SORT d.@value2 @value3\n          LIMIT @value4, @value5\n          RETURN d\n      ", "bindVars" : { "value0" : "title_en", "value1" : "1", "value2" : "created_at", "value3" : "ASC", "value4" : 0, "value5" : 10 } })
JavaScript exception in file '/usr/local/Cellar/arangodb/2.8.6/share/arangodb/js/client/modules/org/arangodb/arangosh.js' at 106,13: ArangoError 1552: bind parameter 'value1' was not declared in the query (while parsing)
!      throw error;
!            ^
stacktrace: ArangoError: bind parameter 'value1' was not declared in the query (while parsing)
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/2.8.6/share/arangodb/js/client/modules/org/arangodb/arangosh.js:104:21)
    at ArangoStatement.execute (/usr/local/Cellar/arangodb/2.8.6/share/arangodb/js/client/modules/org/arangodb/arango-statement.js:186:12)
    at ArangoDatabase._query (/usr/local/Cellar/arangodb/2.8.6/share/arangodb/js/client/modules/org/arangodb/arango-database.js:814:45)
    at <shell command>:1:4

我做错了吗?请帮忙。谢谢!

PS: OS X上的Arangodb v2.8.6

1 个答案:

答案 0 :(得分:8)

您的错误是您在绑定值周围加上引号。绑定值不能用引号设置。

由于您正在使用模板字符串,因此会自动为绑定值指定名称:valueN。模板字符串引擎将您的查询转换为以下内容:

db._query(`
  FOR d IN FULLTEXT('api_texts', '@value0', '@value1')
    SORT d.@value2 @value3
    LIMIT @value4, @value5
    RETURN d
`, {value0: args.attribute, value1.... }).toArray()

我们最好通过调用模板处理器来检查这一点,而不将结果直接放入db._query()

args={
    attribute: "theAttribute", 
    search: "theSearchString", 
    sort_by: "sortByMe", 
    skip: 5, 
    limit: 10
}
aqlQuery`FOR d IN FULLTEXT('api_texts', '${args.attribute}', '${args.search}')
    SORT d.${args.sort_by} ${args.sort} LIMIT ${args.skip}, ${args.limit} RETURN d`
{ 
  "query" : "FOR d IN FULLTEXT('api_texts', '@value0', '@value1') 
             SORT d.@value2 @valu...", 
  "bindVars" : { 
    "value0" : "theAttribute", 
    "value1" : "theSearchString", 
    "value2" : "sortByMe", 
    "value3" : undefined, 
    "value4" : 5, 
    "value5" : 10 
  } 
}

由于绑定值不能包含在字符串中(否则它们被视为字符串),您的查询将不会引用@value0@value1,这就是错误所在:

绑定值列表中指定的绑定值,查询未引用该值。

更正了${...}附近没有引号的代码:

db._query(aqlQuery`
  FOR d IN FULLTEXT('api_texts', ${args.attribute}, ${args.search})
    SORT d.${args.sort_by} ${args.sort}
    LIMIT ${args.skip}, ${args.limit}
    RETURN d
`).toArray()