繁体   English   中英

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

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

我已经做了几个月的arangojs,并且最近我的一些代码开始失败,过去工作,关于在使用模板字符串和aqlQuery时使用变量作为集合名称。

我在Windows 10上使用Node.js 4.4.0。

此示例代码显示错误:

// 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)
});

我在干净的npm安装上测试了这个,只有arangojs 4.3.0和Node 4.4.0或Node 5.5.0(使用nodist)。

有没有其他人在模板字符串中看到集合名称的问题?

arangojs文档没有明确指出这一点,AQL文档掩盖了这一点,但集合绑定变量是专门处理的。 手动编写AQL查询时,这意味着您需要在bindVars使用@前缀其名称,并相应地使用@@前缀而不是通常的@引用它们。

因为没有办法识别字符串是指字符串值还是集合名称,所以在aqlQuery中使用集合作为绑定变量正确方法是传入arangojs集合对象而不是字符串本身,例如:

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

如果你登录的aqlQuery模板的输出到控制台,你会看到,查询正确插入与参考@@前缀和绑定变量包含@它的值是集合(而不是集合对象的名称前缀变量你过去了)。

暂无
暂无

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

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