7

I have such code in my Foxx app:

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()

It seems to be correct but for some reason it gives me an error bind parameter 'value1' was not declared in the query (while parsing).

I tried to run it in arangosh... same error there as well:

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

Am I doing something wrong? Please help. Thanks!

PS: Arangodb v2.8.6 on OS X

artnikpro
  • 5,487
  • 4
  • 38
  • 40

1 Answers1

12

Your error is that you put quotes around the bind values. Bind values mustn't be set in quotes.

Since you're using template strings, the bind values are automatically assigned a name: valueN. The template string engine translates your query into something like this:

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

We better inspect this by calling the template processor without putting the result directly into 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 
  } 
}

Since bind values mustn't be in strings (else they're treated as strings) your query won't reference @value0 and @value1, which is what the error is about:

a bind value specified in the list of bind values, which isn't referenced by the query.

Corrected code without quotes around ${...}:

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()
CodeManX
  • 11,159
  • 5
  • 49
  • 70
dothebart
  • 5,972
  • 16
  • 40