【问题标题】:how foreach obj from api make another request来自api的foreach obj如何发出另一个请求
【发布时间】:2018-01-08 11:54:08
【问题描述】:

我的第一个帖子请求看起来像

this.http.post('http://localhost:8080/api/userfilm/get/', {
      name: this.name })

然后他返回具有名为filmid 的属性的对象数组。我的第二个帖子请求看起来像

this.http.post('http://localhost:8080/api/post/get/', {
        filmid: film.filmid })

我真的不知道我该如何一一做到这一点。例如

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);
      })

但这不是正确返回

【问题讨论】:

    标签: javascript angular typescript ionic3


    【解决方案1】:

    你可以这样实现

    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));
    

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      相关资源
      最近更新 更多