我已经做了几个月的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)
});我已经用arangojs4.3.0和Node4.4.0或Node5.5.0(使用nodist)在一个干净的npm安装上测试了这一点。
其他人见过模板字符串中集合名称的问题吗?
发布于 2016-04-21 08:20:47
arangojs文档并没有明确指出这一点,AQL文档也对此进行了说明,但是集合绑定变量将被专门处理。当手工编写AQL查询时,这意味着您需要在其名称前加上@在bindVars中,并相应地使用@@前缀而不是通常的@引用它们。
由于无法识别字符串是引用字符串值还是集合名称,因此在aqlQuery中使用集合作为绑定变量的正确方法将传递一个arangojs集合对象,而不是字符串本身,例如:
const collection = db.collection(collectionName);
db.query(aqlQuery`
INSERT { "something": ${sampleText} }
IN ${collection}
RETURN NEW._key
`)如果将aqlQuery模板的输出记录到控制台,您将看到查询正确地插入带有@@前缀的引用,绑定变量包含@前缀变量,其值是集合的名称(而不是传入的集合对象)。
https://stackoverflow.com/questions/36761420
复制相似问题