Problem
I'm having an issue with a MERM application whereby a variable which is declared in the body of the whole function is assigned to in a sub function, however, whenever I try to manipulate/display this variable just at the end of the whole function, it returns an "undefined".
Code
getSimilarCasesFromID: function(req, res, next) {
let queryString = "id::"+req.params.id;
let params = {
'query': queryString,
'environment_id':environmentId,
'collection_id': collectionId,
'configuration_id': configurationId,
return: 'enriched_text'
}
let filterStrArr = [];
const FILTER_CONCEPT = "enriched_text.concepts.text:";
let filterStr ="";
discovery.query(params, (error, results) => {
if (error) {
next(false, err, []);
} else {
let conceptSize = results.results[0].enriched_text.concepts.length;
let concepts = {};
for (let i = 0; i < conceptSize; i++) {
concepts[i] = {
text: results.results[0].enriched_text.concepts[i].text,
relevance: results.results[0].enriched_text.concepts[i].relevance
};
filterStrArr[i] = FILTER_CONCEPT + concepts[i].text;
}
filterStr = filterStrArr.join(",");
console.log(filterStr);
//1. Works and displays---------------
}
});
console.log("FullString (2.)"+filterStr);
//2. Undefined????????????????????------------
next(true, [], []);
},
I refer to lines (1.) and (2.). I cannot tell whether I have missed something and made a silly, menial error.
Interestingly, as you see in Figure 1, FullString (2.) appears before line (1.). Could this be due to the response time from Watson Discovery? Bearing in mind that the service is located in Sydney, Australia? And if so, has anyone else had any experience with this?