我期待做一个get,在结果上运行一个函数,它将通过更新一个字段来进行一些操作,然后将该文档回到数据库中。真的,我的问题是能够链接多个数据库调用。过去一周左右我一直在努力。任何建议表示赞赏,谢谢。从rethinkdb数据库获取数据,处理所述数据,然后用操纵的文档更新数据库
这是我迄今为止尝试过,但是我收到一个错误:
function geocode_cleanup(request, response, next) {
r.table('dealer_locations').filter(r.row('geodata').match('No geodata found.'))
.do(function(row) {
var geodata = opencage_geocoder.geocode(row.Address, function(error, response) {
if (error) {
console.log("Error.");
row.geodata = "No geodata found.";
row.active = true;
} else if (response.length == 0) {
console.log("Empty response.");
} else {
console.log("Success.");
console.log(response);
var latitude = response[0].latitude;
var longitude = response[0].longitude;
row.geodata = r.point(longitude, latitude);
row.active = true;
}
});
return r.table('dealer_locations').update({
geodata: geodata
})
}).run(conn, function(error, cursor) {
response.setHeader("Content-Type", "application/json");
if (error) {
handleError(response, error);
} else {
cursor.toArray(function(error, results) {
if (error) {
handleError(response, error);
} else {
response.send(results);
};
});
}
next();
})
};
而且,这给在响应中返回了预期的效果,但第二个分贝行动从未发生过,因为我还在里面我认为相同的db连接:
function geocode_cleanup(request, response, next) {
var conn = request._rdbConn;
r.table('dealer_locations').filter({geodata: "No geodata found."}).run(conn, function(error, cursor) {
if (error) {
handleError(response, error);
} else {
cursor.toArray(function(error, results) {
if (error) {
handleError(response, error);
} else {
var i = 1;
async.forEach(results, function(item, callback) {
var address = (item.Address + " " + item.City).toString();
opencage_geocoder.geocode(address, function(err, res) {
if (err) {
console.log(i);
console.log("Error.");
item.id = i;
item.geodata = "No geodata found.";
item.active = true;
i++;
callback();
} else if (res.length == 0) {
console.log(i);
console.log("Empty response.");
i++;
callback();
} else {
console.log(i);
console.log("Success.");
console.log(res);
var latitude = res[0].latitude;
console.log(i + " " + latitude);
var longitude = res[0].longitude;
console.log(i + " " + longitude);
item.id = i;
item.geodata = r.point(longitude, latitude);
item.active = true;
i++;
callback();
}
});
}, function() {
r.table('dealer_locations').insert(results, {
conflict: "replace"
}).run(request._rdbConn, function(error, results) {
if (error) {
console.log("Data not inserted!");
} else {
console.log("Data inserted!");
}
});
console.log("Done!");
response.send(results);
});
}
})
}
})
}
哪一个是从未发生过的第二个分贝行动? –
插入。 。 'r.table( 'dealer_locations')INSERT(结果,{ 冲突: “代替” })运行(request._rdbConn,功能(错误,结果){ 如果(错误){ 的console.log。 (“Data not inserted!”); } else { console.log(“Data inserted!”); } });' –
您在第一个错误中遇到什么错误?请记住,例如,您不能在ReQL的匿名函数中(例如'do')运行JavaScript函数。这些函数是在服务器上发送和执行的(例如,它不能访问'opencage_geocoder') –