Hi all,
I would like to write a function which returns the price of an Item. I get this information from a webservice. I can’t use HTTP Request service as I require 'sendImmediately': false
.
So the following code returns me the price. But only for one Item:
function getPriceOfItem(articleNumber) {
var user = 'apiuser';
var pass = 'apikey';
var opts = {
method: 'GET',
url: 'http://example.com/api/articles/' + articleNumber + '?useNumberAsId=true',
json: true,
auth: {
'user': user,
'pass': pass,
'sendImmediately': false
},
};
var price;
utils.request(opts, function (err, resp, body) {
if (err) {
return reject(err);
}
price = body.data.mainDetail.prices[0].price
//This returns the price. But I would like to give it back to the function as an return
resolve(price)
});
}
getPriceOfItem('SW10002')
So this is not working:
function getPriceOfItem(articleNumber) {
var user = 'apiuser';
var pass = 'apikey';
var opts = {
method: 'GET',
url: 'http://example.com/api/articles/' + articleNumber + '?useNumberAsId=true',
json: true,
auth: {
'user': user,
'pass': pass,
'sendImmediately': false
},
};
var price;
utils.request(opts, function (err, resp, body) {
if (err) {
return reject(err);
}
price = body.data.mainDetail.prices[0].price
});
return price
}
//This should return the sum of the two items. But it actually returns nothing.
resolve(getPriceOfItem('SW10002') + getPriceOfItem('SW10003'))
So the question is: How can I get the price with the function? I guess I need to work with some callbacks. But I don’t get it
Regards
Sebastian