如果ArangoDB函数db.Query(ctx, query, bindVars)
不返回数据,可能有以下几种解决方法:
bindVars
的参数正确无误。查询语句可能存在错误或者绑定变量的值不正确,导致查询结果为空。query := "FOR doc IN collection FILTER doc.name == @name RETURN doc"
bindVars := map[string]interface{}{
"name": "John",
}
cursor, err := db.Query(ctx, query, bindVars)
if err != nil {
log.Fatal(err)
}
defer cursor.Close()
ctx
):确保上下文(ctx
)被正确传递给db.Query
函数。上下文可能被取消或超时,导致查询无法返回结果。ctx := context.Background()
query := "FOR doc IN collection FILTER doc.name == @name RETURN doc"
bindVars := map[string]interface{}{
"name": "John",
}
cursor, err := db.Query(ctx, query, bindVars)
if err != nil {
log.Fatal(err)
}
defer cursor.Close()
query := "FOR doc IN collection FILTER doc.name == @name RETURN doc"
bindVars := map[string]interface{}{
"name": "John",
}
cursor, err := db.Query(ctx, query, bindVars)
if err != nil {
log.Fatal(err)
}
defer cursor.Close()
if cursor.Count() == 0 {
log.Println("No data found")
} else {
for cursor.HasMore() {
var doc MyStruct
_, err := cursor.ReadDocument(ctx, &doc)
if err != nil {
log.Fatal(err)
}
// Process the document
}
}
通过检查查询语句、绑定变量、上下文、集合和索引,可以解决ArangoDB函数db.Query(ctx, query, bindVars)
不返回数据的问题。