I am using the request-promise npm package to make multiple api calls in nodeJS. In order for me to get the data I require, I would have to cycle through all categories and then in each category cycle through the array of products to get the product info. Sample category response is as below
{
"success": true,
"response": [
{
"category_id": 9,
"name": "Leather Wheeled luggage",
"products": [
"TL141911",
"TL141888"
],
"parent_category_id": 34
},
{
"category_id": 10,
"name": "Leather Luggage Weekender Duffles bags",
"products": [
"TL141794",
"TL141658"
],
"parent_category_id": 34
}
}
Because I have to loop through and make api calls, I am trying to use Promise.all, but it is not producing the correct results, it is throwing me a 404 not found error, can someone please help me where i am going wrong here? below is what i have tried
const rp = require('request-promise');
const requestUrl = "https://stage.tuscanyleather.it/api/v1/";
const categoryRequestUrl = requestUrl + 'categories';
let tuscanApiOptions = {
uri: categoryRequestUrl,
headers: {
'Authorization': 'Bearer asd343'
},
method: 'Get',
json: true
};
rp(tuscanApiOptions)
.then((categResponse) => {
//res.end(categResponse);
let ps = [];
categResponse.response.forEach((category) => {
if (category.products !== undefined) {
category.products.forEach((product) =>{
let productApiOptions = {
uri: requestUrl + `product-info?code=${product}`,
headers: {
'Authorization': 'Bearer asd343'
},
method: 'Get',
json: true
};
ps.push(rp(productApiOptions));
});
Promise.all(ps)
.then((values) => {
console.log(values); //This is where things are going wrong
})
.catch((err) =>{
console.log(JSON.stringify(err,null,2));
})
}
})
})
.catch((error) => {
console.log(error);
//res.status(error.statusCode).send(error.error.error_description);
});