6

我的 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

4

1 回答 1

11

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

由于您使用的是模板字符串,因此绑定值会自动分配一个名称: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 
  } 
}

由于绑定值不能在字符串中(否则它们被视为字符串),您的查询将不会引用@value0and @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()
于 2016-04-18T11:36:15.737 回答