简体   繁体   English

Angular 方法返回 http observable

[英]Angular method return with http observable

I am working on a function to retrieve location information from an API.我正在开发一个从 API 检索位置信息的函数。 As the location data have a hierarchical structure I am trying to implement a recursive method called build list which will eventually give me an object with all the child nodes included inside.由于位置数据具有层次结构,我正在尝试实现一个名为 build list 的递归方法,该方法最终会给我一个包含所有子节点的对象。 ( I am using strapi and it only gives me a single level relational information and I cannot limit the no of levels the system will have). (我使用的是 Strapi,它只给我一个单一级别的关系信息,我不能限制系统将拥有的级别数)。

  buildList(location:string) :Task{
    this.task.getLocations(location).subscribe(data=>{
      return data;
    });
    return null;
  }

Following is the task structure for reference,以下是供参考的任务结构,

export interface Task {
    task_name: string;
    task_description:string;
    role_level: number;
    location:string;
    children:Task[]
    id: string;
  }

My issue is that whenever I call the buildList function, it will return null as the getLocation function is based off of angulars http get method which returns an observable.我的问题是,每当我调用 buildList 函数时,它都会返回 null,因为 getLocation 函数基于返回 observable 的 angulars http get 方法。 I am expecting to implement a recursive function inside but it keeps giving me null as it executes return null before the http request.我期望在内部实现一个递归函数,但它一直给我 null 因为它在 http 请求之前执行 return null。 I also cannot remove the return null as typescript gives me the error not all code paths return a value .我也无法删除 return null 因为打字稿给了我错误并非所有代码路径都返回值 Any suggestions to make this work will be really appreciated.任何使这项工作的建议将不胜感激。

Thank You谢谢你

I think you can use a global list to achieve your goal and then recursively call buildList func.我认为您可以使用全局列表来实现您的目标,然后递归调用 buildList func。

  locations:any=[]
  buildList(location:string) :void{
    this.task.getLocations(location).subscribe(data=>{
      this.locations.push(data.location);
      buildList(data.location)
    });
  }

Your method always will return null because as you know http client is asynchronous.你的方法总是会返回 null 因为你知道 http 客户端是异步的。 So, if you want to make a "recursive" function a simple aproach could be to have nested calls as you need (no recursive way) using some library as RxJs than can help to you to wait to have all nested queries to return the result.因此,如果您想创建一个“递归”函数,一个简单的方法可能是根据需要(无递归方式)使用某些库作为 RxJ 进行嵌套调用,这样可以帮助您等待所有嵌套查询返回结果.

A pseudo example could be:一个伪示例可能是:

 import { forkJoin } from 'rxjs';
 ...
 buildList(location:string) :Task{
    this.task.getLocations(location).subscribe( mainData => {

        const requests = [];
        mainData.<list_of_items>.forEach(item => {        
          requests.push(this.<yourNestedService>.someCall());
        });
        forkJoin<YourNestedTypeExpected>(requests).subscribe(responses => {
           responses.forEach(response {
              // Your success responses.
              // You can build here the structure that you expect.
          })
          }), error => {
           // There are some error in some query.
           console.warn(error);
         });
      
    });
  }

Maybe can be more complex that it but could be a beginning.也许可能比它更复杂,但可能是一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM