Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 使用ES6文本模板的动态AQL查询_Templates_Ecmascript 6_Arangodb_Aql - Fatal编程技术网

Templates 使用ES6文本模板的动态AQL查询

Templates 使用ES6文本模板的动态AQL查询,templates,ecmascript-6,arangodb,aql,Templates,Ecmascript 6,Arangodb,Aql,我想在Foxx中动态生成一个查询。filter语句是否存在取决于请求参数。比如说 //Conditionally determine if we should include a filter statement here or not var var1=true; getAllEntitiesThatSatisfyTheCondition = db._query(aql ` For u In ${EntityCollection} ${var1 ? `Filter u.

我想在Foxx中动态生成一个查询。filter语句是否存在取决于请求参数。比如说

//Conditionally determine if we should include a filter statement here or not
var var1=true;
getAllEntitiesThatSatisfyTheCondition = db._query(aql `
      For u In ${EntityCollection}
      ${var1 ? `Filter u.prop == ${var1}`:``}
      Return DISTINCT u._id
    `).toArray();
这将返回一个关于绑定值的错误

syntax error, unexpected bind parameter

我如何在arango中以这种方式使用文本模板构造查询,或者我必须使用查询生成器?

这对我使用arango 3.3.16在Foxx上的工作非常有效:

var test = "1124852" 
const filter = aql.literal(
    test ? `AND v._key == "${test}" ` : ''
  );
然后在您的查询中添加

${filter}

你也可以像下面那样使用它

var filter = "FILTER a.name=='test'";


var query = `
   for a IN collection1
  "${filter}"
 limit 50
  return a
`;

db._query(query).toArray();

在这里,javascript过滤器变量将被动态替换。希望这会有所帮助。

ArangoJS驱动程序中还有一些尚未发布的更改应该会对您有所帮助,即将省略未定义的处理,而不是生成没有值的绑定变量和嵌套支持:Related:ArangoJS v6.7.0支持模板的嵌套,但没有说明文字不会验证参数,因此,请谨慎使用并正确验证以避免注入当我将您的建议用于我上面的确切查询时,出现了一个新问题,如AQL:语法错误,意外赋值接近…不管怎样,这是使用的错误!==。我已经接受了你的回答