Thursday, 8 June 2017

How to import string from require in nodeJS when imported function takes time to finish?

I am calling following code which imports a search query function

var match_all = require('./match_all');                                                                                                                                                                                                        
var data=match_all.func();                                                                                                                                                                                                                     
console.log('uo yoyo');                                                                                                                                                                                                                        
console.log(data);

Match_all is as follows:

var string_of_da;                                                                                                                                                                                                                               

//a function which queries for all pid and syscalls                                                                                                                                                                                            
var string = function(){                                                                                                                                                                                                                       
var client = new elasticsearch.Client({                                                                                                                                                                                                        
      .....
    })

//queries to ES                                                                                                                                                                                                                                
function getmeres(client,syscall){                                                                                                                                                                                                             
        client.search({                                                                                                                                                                                                                        
         ........                                                                                                                                                                                                                        

},function (error,response) {                                                                                                                                                                                                                  
      .......                                                                                                                                                                                                                            
showdocs(d)                                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                              
});                                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                                                                                                                                                                                                      
function showdocs(d){                                                                                                                                                                                                                          
var da = d["hits"]["hits"].map(function(i){                                                                                                                                                                                                    
        return i['_source'];                                                                                                                                                                                                                   
});                                                                                                                                                                                                                                            
string_of_da = JSON.stringify(da,null,'\t');                                                                                                                                                                                                   
}                                                                                                                                                                                                                                              
return string_of_da;                                                                                                                                                                                                                           
};                                                                                                                                                                                                                                             
module.exports.func = string;
module.exports.string = string_of_da;

Now when I execute my earlier js program, it queries properly, but before match_all.func() is called and returns string, console.log is called. So my output looks like

uo yoyo

undefined

...elasticsearch search logs...

what is going wrong here?



via Vintux

No comments:

Post a Comment