简体   繁体   English

使用arangojs和aqlQuery在ArangoDb中将集合名称用作变量时出错

[英]Error when using collection names as variables in ArangoDb with arangojs and aqlQuery

I've been arangojs for a few months and recently had some of my code start failing that used to work, regarding the use of variables as collection names when using template strings and aqlQuery. 我已经做了几个月的arangojs,并且最近我的一些代码开始失败,过去工作,关于在使用模板字符串和aqlQuery时使用变量作为集合名称。

I'm using Node.js 4.4.0 on Windows 10. 我在Windows 10上使用Node.js 4.4.0。

This example code shows the error: 此示例代码显示错误:

// Issue with template string and arangodb with collection names
var arango = require('arangojs');
var aqlQuery = require('arangojs').aqlQuery;
var db = new arango('http://127.0.0.1:8529');

var collectionName = 'sampleCollection';

// Connect to _system
db.useDatabase('_system');

var sampleText = "Testing Only";

/* Add a record to the collection, using template string with collectionName
 * If I use ${collectionName} I get the error code 400:
 * error: 'bind parameter \'bind parameter \'value1\' has an invalid value or type near 
 * \'RETURN NEW._key\n
 * \' at position 4:9\' has an invalid value or type (while parsing)',
 *
 * If I write out the collection name, or use a normal db.query style query without using 
 * aqlQuery and template strings and pass the table name as @collectionName, then it works
 * fine.
 */
db.query(aqlQuery`
    INSERT { "something": ${sampleText} }
    IN ${collectionName}
    RETURN NEW._key
`).then(cursor =>
  cursor.all()
).then(vals => {
  console.log(`Created a new document with a _key of ${vals[0]}`)
}).catch(err => {
  console.log(err)
});

I've tested this on a clean npm install with only arangojs 4.3.0 and either Node 4.4.0 or Node 5.5.0 (using nodist). 我在干净的npm安装上测试了这个,只有arangojs 4.3.0和Node 4.4.0或Node 5.5.0(使用nodist)。

Has anyone else seen issues with collection names in template strings? 有没有其他人在模板字符串中看到集合名称的问题?

The arangojs documentation doesn't explicitly point this out and the AQL documentation glosses over this, but collection bind variables are treated specially. arangojs文档没有明确指出这一点,AQL文档掩盖了这一点,但集合绑定变量是专门处理的。 When writing AQL queries by hand this means you need to prefix their names with an @ in the bindVars and accordingly reference them with a @@ prefix instead of the usual @ . 手动编写AQL查询时,这意味着您需要在bindVars使用@前缀其名称,并相应地使用@@前缀而不是通常的@引用它们。

Because there's no way to recognize whether a string refers to a string value or the name of a collection, the correct way to use collections as bind variables in aqlQuery is to pass in an arangojs collection object instead of the string itself, eg: 因为没有办法识别字符串是指字符串值还是集合名称,所以在aqlQuery中使用集合作为绑定变量正确方法是传入arangojs集合对象而不是字符串本身,例如:

const collection = db.collection(collectionName);
db.query(aqlQuery`
  INSERT { "something": ${sampleText} }
  IN ${collection}
  RETURN NEW._key
`)

If you log the output of the aqlQuery template to the console you will see that the query correctly inserts a reference with the @@ prefix and bind variables contains the @ prefixed variable with its value being the name of the collection (rather than the collection object you passed in). 如果你登录的aqlQuery模板的输出到控制台,你会看到,查询正确插入与参考@@前缀和绑定变量包含@它的值是集合(而不是集合对象的名称前缀变量你过去了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM