1

i want to handle dynamic query string in Arangodb

let condition = 'FILTER u.username == '+value

if(usingPhoneNumber){
   condition = 'FILTER u.phoneNumber == '+value
}

const query = aql`
   FOR u in users
      ${condition}
   RETURN u
`

if I do like this, I'm getting error like

ArangoError: AQL: syntax error, unexpected bind parameter near '@value0

4 Answers 4

1

we can pass aql query into another aql query

let condition = aql`FILTER u.username == ${value}`

if(usingPhoneNumber){
   condition = aql`FILTER u.phoneNumber == ${value}`
}

const query = aql`
   FOR u in users
      ${condition}
   RETURN u
`

this fixes my issue. thank guys for your answers

Sign up to request clarification or add additional context in comments.

Comments

0

I guess your username and phone number are strings, so they must be between quotes.

let condition = 'FILTER u.username == "' + value + '"'

if(usingPhoneNumber){
   condition = 'FILTER u.phoneNumber == "' + value '"'
}

const query = aql`
   FOR u in users
      ${condition}
   RETURN u
`

1 Comment

the issue is not with 'value'. if we add any query(filter condition) using a variable like above then AQL is treating that as a string. And throwing bind variable error
0

Could you try something like

let condition = 'FILTER u.username == ${value}'

if(usingPhoneNumber){
   condition = 'FILTER u.phoneNumber == ${value}'
}

Assuming the value is getting something assigned, I feel this might work. Do let me know the error that comes up, in case it comes up.

Comments

0

I suggest you try adding the condition in the query.

 FOR u in users
      FILTER u.username ==${value0} OR  u.phoneNumber == ${value0}
 RETURN u

If this works for you, I supose the problem is the aql formater assumes all inserted values with ${} are bind parameters, wich will be iterated over and not a constant query declaration.

Your current script resolves to:

{ 
  "query" : "FOR u IN users @value0 RETURN c._key", 
  "bindVars" : { 
    "value0" : "'FILTER u.username == '+value" 
  } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.