我正在尝试从函数中获取count
值,并希望存储在函数外部。
var count;
client.count({
index: 'employee',
type: 'details',
}, function (err, response) {
if(err){console.log(err)}
count = response.count
console.log(count);
});
console.log(count); 我想在函数之外使用这个计数。 这可能吗?
答案 0 :(得分:3)
尝试以这种方式编写代码:
client.count({
index: 'employee',
type: 'details',
}, afterCount);
function afterCount(err, count){
if (err) console.log(err);
// do whatever you want with count
// if there's too much logic, split it into functions and send count as a param to them
}
答案 1 :(得分:2)
包装函数或使用Promise API重构代码。
select ownerId, Count(ownerId)
from Apartment
group by ownerId