I am getting output as undefined
in the last line of code. Ideally it should print the value of cost
variable which I have assigned in the last line of call function
. cost variable
is printing value correctly. what mistake I am making. or how can I access value of cost
outside the function
.
var request = require("request");
var itemCost;
function call() {
request.get("http://localhost:8080/ords/hr/rest-v3/item/Sugar", (error, response, body) => {
if (error) {
return console.dir(error);
}
let jsonData = JSON.parse(response.body);
let obj = new Object(jsonData);
obj.items.forEach(itemChild => {
let cost = itemChild.itemcost;
console.log(cost);
itemCost = cost; //Assigning value of cost to itemcost. is this correct way?
})
});
}
call();
console.log(itemCost); //here I am getting undefined. I want value of cost here.