I am running Express.js/Node.js application with ElasticSearch. I am trying to view results from multiple types in the same index. What I do here is run a search query and based the results of the query second search query executes. I can see that elasticsearch return results for players also by observing the node.js console. But they are not inserted to my results object/array. I am using express middleware since I have to execute two search and display results as one.
routes/index.js
function searchTeam(req, res, next){
searchModuleTeams.searchTeams(req.body, function(data) {
req.teams = data;
next();
});
}
function searchPlayer(req, res, next){
//req.players = [];
req.teams.forEach(function(team){
req.body = {searchTerm:team._source.shortName};
searchModulePlayers.searchPlayers(req.body, function(data){
req.players.push(data);
console.log(req.players);
});
});
next();
}
function renderResults(req, res){
res.render('index',{
title:'Search Teams and Players',
teams:req.teams,
players:req.players
});
}
router.post('/search-tp',searchTeam, searchPlayer, renderResults);
I came up with this solution by reading post1 and post2. I can display the teams array. But nothing comes from the players array. What am I doing incorrect in here.