[英]how foreach obj from api make another request
my first post req looks like 我的第一个帖子要求看起来像
this.http.post('http://localhost:8080/api/userfilm/get/', {
name: this.name })
and he returns array of objects with property named filmid. 然后他返回具有名为filmid的属性的对象数组。 my second post request looks like
我的第二个帖子请求看起来像
this.http.post('http://localhost:8080/api/post/get/', {
filmid: film.filmid })
i really dont know how i can do this one by one. 我真的不知道我该怎么做。 for example
例如
getAllPosts() {
return this.http.post('http://localhost:8080/api/userfilm/get/',
{ name: this.name })
.flatMap((film: any) =>
this.http.post('http://localhost:8080/api/post/get/',
{ filmid: film.filmid }))
}
this.getAllPosts().subscribe(response => {
console.log(response);
})
but this is not returns corretly 但这不是正确地返回
You can implement like this 你可以这样实现
this.http.get("https://jsonplaceholder.typicode.com/users")
.map(res => res.json())
.mergeMap((customers: any[]) => {
if (customers.length > 0) {
return Observable.forkJoin(
customers.map((customer: any) => {
return this.http.get('https://jsonplaceholder.typicode.com/users/' + customer.id)
.map((res: any) => {
return res.json();
});
})
);
} else {
return Observable.of([]);
}
}).subscribe(res => console.log(res));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.