elasticsearch - Express doesn't wait until the process finish to send response -


i searching classname elasticsearch , searches through 2 types since have linked them id. have call 3 methods whole data need send response,

{ "_index": "classdata", "_type": "classdata", "_id": "59a4e51d6812d73bd8e9ea64", "_score": 1.754925, "_source": { "jarid": "59a4e51d6812d73bd8e9ea60", "classname": "usertransactionmanager", "_id": "59a4e51d6812d73bd8e9ea64", "jarfilename": "cuts-api.jar", "dependencies": [                  {                 "dependentclass": "builderimpl",                 "methodsignature": "<init>()"                 }                 ] } }  { "_index": "jardata", "_type": "jardata", "_id": "59a4e51a6812d73bd8e9e317", "_score": 6.954541, "_source": { "groupid": null, "artifactid": null, "_id": "59a4e51a6812d73bd8e9e317", "jarfilename": "client.jar", "directory": "c:\application", "version": null } } ] } 

for example if give "usertransactionmanager" input first method searches dependencies, in here "builderimpl" , use in second method , jarid. use jarid in third method path.

i have tried in below mentioned way not working. correct way it?

router.post('/test', (req, res) => {  let body = {     size: 30,     from: 0,     query: {         bool: {             must: [                 {                     match: {                         classname: {                             query: req.body.classname                         }                     }                 }             ]         }     } };  search('classdata', body).then(results => {     let nmb = 1;     let messages = [];     results.hits.hits.foreach((hit, index) => hit._source.dependencies.foreach(function (myclass) {             getjarid(myclass.dependentclass).then(message => {                 messages.push(message.pop());                 if ((results.hits.total) === nmb) {                     res.send(messages);                 }                 nmb++;             }).catch(err => {                 res.status(500).send(err);             })         }));       if (results.hits.total === 0) {         res.send(messages);     } })     .catch(function (err) {         console.error(err);         res.status(500).send(err);     }); });  function getdirectories(jarid) { let body = {     size: 20,     from: 0,     query: {         match: {             _id: {                 query: jarid             }         }     } }; return search('jardata', body).then(results => {     let message = [];     results.hits.hits.foreach((hit, index) =>         message.push({directory: hit._source.directory, jarname: hit._source.jarfilename})     );     return message; })     .catch(function (err) {         console.error(err);         throw err;     }); }  function getjarid(csname) { console.log(csname); let resultsize = 30; let body = {     size: resultsize,     from: 0,     query: {         match: {             classname: {                 query: csname             }         }     } }; return search('classdata', body).then(results => {     let messages = [];     results.hits.hits.foreach((hit, index) =>         getdirectories(hit._source.jarid).then(message => {             messages.push(message.pop());         }).catch(err => {             res.status(500).send(err);         }));     return message; }).catch(function (err) {     console.error(err);     throw err; }); } 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -