I am making the following http get call to acquire an html page source code and then parse through it.
For some reason, it keeps returning undefined.
How can I fix it?
Update: It seems that
$http.get()
is asynchronous, so how would I go about returning a value from it?
function checkInventory() {
var newReqObj = DataService.getFormData();
var store = newReqObj.store;
var code = newReqObj.code;
var size = newReqObj.size;
if(store === 'nike') {
var inventoryData = checkNike(store, code, size);
$log.debug(inventoryData);
return inventoryData;
}
}
function checkNike(store, code, size) {
var inventoryData = {
'qtyAvailable': 0,
'maxOrder': 0,
'size': 0,
'name': '',
'price': '',
'url': '',
'code': code
};
var url = 'http://www.nike.co.uk/search?q=' + code;
checkNikeUrl(url, size, inventoryData);
}
function checkNikeUrl(url, size, inventoryData) {
$http.get(url).then(function(response) {
var html = response.data;
inventoryData.qtyAvailable = utility.getProductQty(html, size);
inventoryData.maxOrder = utility.getProductMaxOrder(html, size);
inventoryData.size = size;
inventoryData.name = utility.getProductName(html);
inventoryData.price = utility.getProductPrice(html);
inventoryData.url = url;
return inventoryData;
})
};