简体   繁体   English

api中的foreach obj如何发出另一个请求

[英]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.

相关问题 我正在尝试根据使用 forEach 的另一个 API 请求的结果发出 API 请求。 我如何在不使用 forEach 的情况下存档相同的结果 - I am trying to make an API request based on the result of another API request using forEach. How I could archive the same result without using forEach 在另一个ForEach中互相搏斗 - Make a forEach in another ForEach 如何从 `getStaticProps` 向 API 路由发出请求 - How to make a request to an API route from `getStaticProps` 如何根据另一个请求的响应向 API 发出请求? 不能在回调中调用 React Hook “useSwr” - How can I make a request to an API based on the response from another request? React Hook “useSwr” cannot be called inside a callback 在 foreach 循环中进行 api 调用并将结果推送到另一个数组中 - make an api call in foreach loop and push the result in another array 在foreach中请求api - request api in foreach 如何从另一个网站向node.js应用程序发出请求? - How to make a request to a node.js app from another website? 如何在依赖于另一个异步调用数据的forEach()内部进行异步调用? - How to make asynchronous calls inside a forEach() which depends on data from another asynchronous call? 如何从反应到节点/表达 api 发出发布请求 - How To make a post request from react to node/express api 如何从 Flutter 发出 POST 请求以放大 REST API - How to make a POST request from Flutter to Amplify REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM