gpt4 book ai didi

node.js - rethinkDB .then 对查询结果

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:46 24 4
gpt4 key购买 nike

我将 nodeJS 与 native rethinkDB 驱动程序一起使用,并有一个基本的选择 3 列查询,如下所示:

var runQuery = function() {
var items = [];
r.connect({host: 'someHost',
port:28015,
db:'test'})
.then(function(c){
r.table('ebb1')
.pluck("col1","col2","col3")
.run(c, function(err,cursor){

cursor.each(function(err,result){
if(err) throw err;
items.push(result);
})
})
}).then(console.log(items))
}

我尝试按照重新思考文档的建议使用cursor.toArray(),但当我尝试返回它时,我得到一个空值,我认为结果太大。当我在 cursor.each() 函数中 console.log(result) 时,我确实看到了我期望的结果,因此我相信它们被推送到 items 数组中,但我不确定在完全填充结果数据后如何正确引用列表。

任何帮助或指导将不胜感激!

最佳答案

您正在混合回调样式和 promise ,这使事情变得更加困难,因为您必须在回调中返回 promise 。在您的代码中,在回调完成并记录结果之前会触发 promise 履行。

所以最好还是遵守 promise 。甚至 toArray 也可以返回 Promise。

var runQuery = function() {
r.connect({
host: 'someHost',
port:28015,
db:'test'
})
.then(function(c){
return r.table('ebb1')
.pluck("col1","col2","col3")
.run(c)
})
.then(function(re) {
return re.toArray()
})
.then(function(data) {
//data is the array of your query result
console.log(data)
})
}

关于node.js - rethinkDB .then 对查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com