首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在带有arangojs和ArangoDb的aqlQuery中将集合名称用作变量时出错

在带有arangojs和ArangoDb的aqlQuery中将集合名称用作变量时出错
EN

Stack Overflow用户
提问于 2016-04-21 06:33:31
回答 1查看 894关注 0票数 3

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

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

此示例代码显示错误:

代码语言:javascript
复制
// 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)
});

我已经用arangojs4.3.0和Node4.4.0或Node5.5.0(使用nodist)在一个干净的npm安装上测试了这一点。

其他人见过模板字符串中集合名称的问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 08:20:47

arangojs文档并没有明确指出这一点,AQL文档也对此进行了说明,但是集合绑定变量将被专门处理。当手工编写AQL查询时,这意味着您需要在其名称前加上@bindVars中,并相应地使用@@前缀而不是通常的@引用它们。

由于无法识别字符串是引用字符串值还是集合名称,因此在aqlQuery中使用集合作为绑定变量的正确方法将传递一个arangojs集合对象,而不是字符串本身,例如:

代码语言:javascript
复制
const collection = db.collection(collectionName);
db.query(aqlQuery`
  INSERT { "something": ${sampleText} }
  IN ${collection}
  RETURN NEW._key
`)

如果将aqlQuery模板的输出记录到控制台,您将看到查询正确地插入带有@@前缀的引用,绑定变量包含@前缀变量,其值是集合的名称(而不是传入的集合对象)。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36761420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档