how to access nested data by nesting fetch calls? - json

I'm having trouble understanding the best approach to this.
My goal is to display nested data.
I use fetch on this url - https://horizons-json-cors.s3.amazonaws.com/products.json
which takes me to a page that contains json. inside the json is 3 urls. each url contains the data that I need to get to.
So far, I've accessed the first layer, and now have an array of the item urls. I guess I don't understand how to fetch the data while im inside the outter fetch call.
Here's my code thus far (the result is an array of urls, where each url contains the data I need.) :
componentDidMount() {
console.log('Fetch');
fetch("https://horizons-json-cors.s3.amazonaws.com/products.json")
.then((resp) => (resp.json()))
.then((json) => {
var productUrlArr = [];
for (var i = 0; i < json.length; i++) {
productUrlArr.push(json[i].url);
}
.catch((err) => {
console.log('error', err);
});
}
If you guys could help me out and really walk through how to access the next level of data, I would really, really appreciate it.

You can Fetch Data for Inner URLs this way too,
// 1. Outer Fetch call initiated here
fetch("https://horizons-json-cors.s3.amazonaws.com/products.json")
.then(res => {
return res.json()
})
.then(res => {
// 2. array for storing url's retrieved from response
var urlArray = []
if (res.length > 0) {
// 3. Push url inside urlArray
res.map(data => urlArray.push(data.url))
}
// 4. an array of urls
return urlArray
})
.then(urls => {
// Return an promise which will return "JSON response" array for all URLs.
// Promise.all means “Wait for these things” not “Do these things”.
return Promise.all(urls.map(url => {
// Take url fetch response, return JSON response
return fetch(url).then(res => res.json())
}
))
})
.then(res => {
// Store all objects into array for later use
var objArr = res; return objArr
})
//.then(...)

You have a little error in your code.
It's missing }) before .catch
With it you can use your data in the array.
componentDidMount(){
console.log('Fetch');
fetch("https://horizons-json-cors.s3.amazonaws.com/products.json")
.then((resp) => (resp.json()))
.then((json) => {
var productUrlArr = [];
for (var i = 0; i < json.length; i++) {
productUrlArr.push(json[i].url);
}
console.log(productUrlArr);
}).catch((err) => {
console.log('error', err);
});
}
Hope it helps.

It simple. First get all the url first like you did. Then map and pass it into Promise.all.
fetch("https://horizons-json-cors.s3.amazonaws.com/products.json")
.then((resp) => (resp.json()))
.then((json) => {
Promise.all(json.map(product =>
fetch(product.url).then(resp => resp.text())
)).then(texts => {
// catch all the data
})
}).catch((err) => {
console.log('error', err);
});

Related

How to pass object through GET http param

Im trying to send the object Pack through get like:
save_pack(Pack: any){
return new Promise((resolve, reject) =>{
this.http
.get("http://localhost:8000/save_pack?Pack="+Pack)
.subscribe(res => {
resolve(res);
},
(err: any) => {
reject(err);
}
)
});
}
However, when I try to check if the value of that object is correct into my API rest, for example checking it size, I get just an undefined instend of the value of the size of the object.
app.get('/save_pack', async (req,res) => {
console.log(req.query);
const Pack = req.query.Pack;
console.log("Size de Pack en peticion" + Pack.size)
let result = await save_pack(Pack);
res.send(result)
return res;
})
Im I doing something wrong? Im not sure if an object should be send like this.

forkJoin not working with dynamic array of URLs in Angular 6

import { Observable } from 'rxjs';
import 'rxjs/add/observable/forkJoin';
reqArray = [];
for (let i = 0; i < this.array.length; i++) {
this.reqArray.push(this.companyLocationService.locationsUpdate());
}
forkJoin(this.reqArray).subscribe(
data => {
console.log(data);
},
err => console.error(err)
);
When I pass data without the for loop in a static way, it works fine.
forkJoin(
this.reqArray.push(this.companyLocationService.locationsUpdate(1)),
this.reqArray.push(this.companyLocationService.locationsUpdate(2)),
this.reqArray.push(this.companyLocationService.locationsUpdate(3))
).subscribe(
data => {
console.log(data);
});
But, in my case I would have to create URLs array based on a few conditions, so adding it statically won't be possible.
how can I resolve this problem?
Can you put below line of codes inside a function and call that function after this.reqArray.push(this.companyLocationService.locationsUpdate()) line:-
forkJoin(this.reqArray).subscribe(
data => {
console.log(data);
},
err => console.error(err)
);
Hope this helps you out.

Parsing json response gives undefined

I am using angularfire2 to fetch data from realtime DB. and response comes like below
{"type":"value",
"payload":{
"-LJXFAd_q3Cin64EBc7H":
{"_date":"9-8-2018",
"_deliveryType":"Pick up",
"_estDeliveryTime":"2018-08-10T11:43:57.164Z",
"_location":""}
}}
this on doing
the element inside payload is a key created using push. I wont know it to refer so how do i get the data under "LJXFAd_q3Cin64EBc7H" ?
there are many such entries inside payload and i need to fetch all.
code used to get above is:
getOrderHistory(uid:string){
console.log('start of getOrderHistory with uid:' + uid)
return new Promise((resolve, reject) =>
{
this.db.object("/users/" + uid + "/orders").snapshotChanges().subscribe(
res => {
//console.log('response:' + res)
resolve(res)
},
err => {
console.log(err)
reject(err)
}
)
})
}
Try this, it maps each snapshot to an object that holds its key and values.
this.db.object("/users/" + uid + "/orders").snapshotChanges()
.map(snapshot => {
const key = snapshot.key;
const data = snapshot.payload.val();
return { key, ...data };
})
.subscribe(res => {
resolve(res);
},
err => {
console.log(err);
reject(err);
});
normally, realtime database sends snapshot where you can do snapshot.id to get the id and .data (or .data() I forget at the moment) to get at the payload.
JSON.parse it then iterate it or get at it with dot notation or bracket notation. To get a property:
obj[payload][LJXFAd_q3Cin64EBc7H]
To iterate it:
function eachRecursive(obj)
{
for (var k in obj)
{
if (typeof obj[k] == "object" && obj[k] !== null)
eachRecursive(obj[k]);
else
// do something...
}
}

How to render 2 json's in JSX

I have 2 json's which I get on making 2 different API calls.
fetch(`veb/api/roleDetails/${id}`).then(response =>
response.json()
).then(responseJson => {
console.log('responseJson = ' + JSON.stringify(responseJson));
this.setState(() => {
return {
allRoleDetails: responseJson,
}
});
}
).catch(error => console.error('Error:', error))
2nd:
fetch(`/api/api/itemTypes`).then(response =>
response.json()
).then(responseJson => {
console.log('responseJson = ' + JSON.stringify(responseJson));
this.setState(() => {
return {
names: responseJson,
}
});
}
).catch(error => console.error('Error:', error))
in the 1st api call I get itemtypeid from which I have to make the 2nd api call to fetch the name of the typeid.Even if I get a combined json it will be fine
How to do that?Any help will be great.
Thanks
Promise.all is what you need. Basically, what it does is merging a collection of promises into one single promise, and return the resolved values one everything inside the collection done.
Promise.all([
fetch(`veb/api/roleDetails/${id}`).then(response => response.json()),
fetch(`/api/api/itemTypes`).then(response => response.json())
]).then(([allRoleDetails, names]) => {
// allRoleDetails is the resolve of your first promise
// names is the resolve of your second promise
}).catch(err => {
// catch error here
});
More details about Promise.all can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Ionic 2 - Passing ID from json to child (details) page

I have a provider service that calls get requests from my API. I then have a listing page whereby you can scroll though many recipes. What I am struggling with is taking the ID of each recipe and passing it to the details page as this needs to be included within.
My service request is for the listing is
loadCategory1() {
var url = "http://api.yummly.com/v1/api/recipes?_app_id=////&_app_key=////";
if (this.Category1) {
return Promise.resolve(this.Category1);
}
return new Promise(resolve => {
this.http.get(url + "&allowedAllergy[]=396^Dairy-Free&allowedAllergy[]=393^Gluten-Free&maxResult=50&start=10")
.map(res => res.json())
.subscribe(data => {
console.log(data);
this.Category1 = data.matches;
resolve(this.Category1);
});
});
}
and I currently have a separate one for my details as well
loadDetails() {
if (this.details) {
return Promise.resolve(this.details);
}
return new Promise(resolve => {
this.http.get('http://api.yummly.com/v1/api/recipe/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364?_app_id=//////&_app_key=//////')
.map(res => res.json())
.subscribe(data => {
console.log(data);
this.details = data;
resolve(this.details);
});
});
}
As you can see in the details request i have French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364 This needs to be dynamic by taking the ID from each recipe. Example is below.
Within each .ts file I have the following
loadRecipes(){
this.apiAuthentication.loadCategory1()
.then(data => {
this.api = data;
});
}
This allows me to call the request.
I'm at the point now where I have no clue what to do so some help would be great.
Your DetailsService can be something like this:
loadDetails(detailsId: string) {
return new Promise(resolve => {
this.http.get('http://api.yummly.com/v1/api/recipe/'+detailsId+'?_app_id=//////&_app_key=//////')
.map(res => res.json())
.subscribe(data => {
console.log(data);
this.details = data;
resolve(this.details);
});
});
}
Navigate to DetailsPage with arguments:
this.navCtrl.push(DetailsPage,{
recipe: recipe
});
And you can call DetailsService inside DetailsPage by using code like this:
loadDetails(){
this.apiAuthentication.loadDetails(this.recipe.id)
.then(data => {
this.details = data;
});
}