问题:
为什么变量( resultVisitor )返回undefined?
当我在函数 searchVisitors()中记录命中时,日志返回一个对象数组?
任何想法?
/* Get passanten telling */
var searchVisitorParams = {
index: 'veenendaal',
type: 'passanten',
size: 100,
body: {
fields: ["Tijdsperiode", "201_WE_Veenendaal", "940_HEMA_Veenendaal"],
query: {
"match_all": {}
},
sort: {
Tijdsperiode: "asc"
}
}
};
function searchVisitors() {
client.search(searchVisitorParams).then(function (body) {
var hits = body.hits.hits;
console.log(hits)
return hits;
});
}
var resultVisitor = searchVisitors();
console.log(resultVisitor)
答案 0 :(得分:1)
返回在回调函数中。我会做这样的事情:
/* Get passanten telling */
var searchVisitorParams = {
index: 'veenendaal',
type: 'passanten',
size: 100,
body: {
fields: ["Tijdsperiode", "201_WE_Veenendaal", "940_HEMA_Veenendaal"],
query: {
"match_all": {}
},
sort: {
Tijdsperiode: "asc"
}
}
};
function searchVisitors(callback) {
client.search(searchVisitorParams).then(function (body) {
var hits = body.hits.hits;
callback(hits);
});
}
searchVisitors(function(hits){ // Results are inside hits variable
console.log(hits);
// .... Your code ... //
});