如何处理服务延迟的数据?

2024-02-19

在我的角度应用程序中,我需要将数据存储到一个数组中,该数组在初始阶段为空。

Example:

someFunction() {

 let array = [];

 console.log("step 1");

 this.service.getRest(url).subscribe(result => { 

   result.data.forEach(element => {

   console.log("step 2");

    array.push(element); // Pushing all the objects comes from res.data     

   });

   console.log("step 3");

 });

   console.log("step 4");

}

我在这里列出了console.log()与步骤顺序。

其中调用该函数的顺序是,

Step 1 Step 4第2步 步骤3

在步骤 1 之后,步骤 4 调用,然后调用步骤 2.. 所以如果我console.log(array)代替步骤 4,它再次给出空数组。

但代替step 2 and 3它提供了价值。从服务中出来后,价值是空的。

因此我总是得到空值array.

请帮助我将数据存储到变量中,即使服务调用和响应返回有一段时间。

修改代码尝试了好久,还是不行。

Edit:

我在下面给出了我当前正在使用的实时应用程序堆栈闪电战 link https://stackblitz.com/edit/angular-x4a5b6-ng8m4z https://stackblitz.com/edit/angular-x4a5b6-ng8m4z

在此演示中查看文件https://stackblitz.com/edit/angular-x4a5b6-ng8m4z?file=src%2Fapp%2Fquestion.service.ts https://stackblitz.com/edit/angular-x4a5b6-ng8m4z?file=src%2Fapp%2Fquestion.service.ts

我在哪里使用服务电话..如果我把async getQuestions() {},它给出的错误是questions.forEach of undefined

In 服务.ts

    jsonData: any = [
    {
      "elementType": "textbox",
      "class": "col-12 col-md-4 col-sm-12",
      "key": "project_name",
      "label": "Project Name",
      "type": "text",
      "value": "",
      "required": false,
      "minlength": 3,
      "maxlength": 20,
      "order": 1
    },
    {
      "elementType": "textbox",
      "class": "col-12 col-md-4 col-sm-12",
      "key": "project_desc",
      "label": "Project Description",
      "type": "text",
      "value": "",
      "required": true,
      "order": 2
    },
    {
      "elementType": "dropdown",
      "key": 'project',
      "label": 'Project Rating',
      "options": [],
      "order": 3
    }
  ];

  getQuestions() {

    let questions: any = [];

    //In the above JSON having empty values in "options": [],

    this.jsonData.forEach(element => {
      if (element.elementType === 'textbox') {
        questions.push(new TextboxQuestion(element));
      } else if (element.elementType === 'dropdown') {

        //Need to push the data that comes from service result (res.data) to the options

        questions.push(new DropdownQuestion(element));

        console.log("step 1");

      //The service which  i call in real time..

        // return this.http.get(element.optionsUrl).subscribe(res => {

        //res.data has the following array, Using foreach pushing to elements.options.

      //   [
      //   { "key": 'average', "value": 'Average' },
      //   { "key": 'good', "value": 'Good' },
      //   { "key": 'great', "value": 'Great' }
      // ],

        // res.data.forEach(result => {
          console.log("step 2");
        //   element.options.push(result);
        // });
        // console.log(element.options) give values as the above [
      //   { "key": 'average'...
        console.log("step 3");
                // console.log(element.options) give values as the above [
      //   { "key": 'average'...
        // });
        console.log("step 4");
      //But here console.log(element.options) gives empty 
      }
    });

    return questions.sort((a, b) => a.order - b.order);
  }

第一步是将函数 getQuestion 转换为 Observable。

为什么有必要?因为您需要调用 this.http.get(element.optionsUrl)。这是异步的(所有 http.get 返回可观察的)。并且需要等待调用完成才能获取数据。可观察的好处是在“订阅函数”中你拥有数据。

因此,我们必须认为“服务返回可观察值,组件订阅服务”。

好吧,让问题。主要问题是我们需要多次调用 http.get。众所周知,所有对 http 的调用都是异步的,所以如何确保我们拥有所有数据(请记住,我们只将数据放入订阅函数中。因为我们不希望有多个订阅 - 最好是拥有没有订阅——在我们的服务中,我们需要使用forkJoin。ForkJoin需要一个调用数组,并返回一个结果数组。

所以首先是创建一个 observable 数组,然后我们返回这个 observable 数组。稍等!我们不想返回一个带有选项的数组,我们想要一个问题的可观察值。为此,尽管返回了 observable 数组,我们还是返回了一个使用该 observable 数组的对象。我在响应的底部放了一个简单的例子

getQuestions():Observable<any[]> { //See that return an Observable

    let questions: any = [];

    //First we create an array of observables
    let observables:Observable<any[]>[]=[];
    this.jsonData.forEach(element => {
      if (element.elementType === 'dropdown') {
        observables.push(this.http.get(element.optionsUrl))
      }
    }
    //if only want return a forkjoin of observables we make
    //return forkJoin(observables)
    //But we want return an Observable of questions, so we use pipe(map)) to transform the response

    return forkJoin(observables).pipe(map(res=>
    {  //here we have and array like-yes is an array of array-
       //with so many element as "dowpdown" we have in question
       // res=[
       //      [{ "key": 'average', "value": 'Average' },...],
       //        [{ "key": 'car', "value": 'dog },...],
       // ],
       //as we have yet all the options, we can fullfit our questions
       let index=0;
       this.jsonData.forEach((element) => { //see that have two argument, the 
                                                  //element and the "index"
          if (element.elementType === 'textbox') {
             questions.push(new TextboxQuestion(element));
          } else if (element.elementType === 'dropdown') {
               //here we give value to element.options
               element.option=res[index];
               questions.push(new DropdownQuestion(element));
               index++;
          }
       })
       return question
    }))
 }

注意:如何使用“of”转换返回可观察值的函数:简单示例

import { of} from 'rxjs';

getData():any
{
   let data={property:"valor"}
   return data;
}
getObservableData():Observable<any>
{
   let data={property:"observable"}
   return of(data);
}
getHttpData():Observable<any>
{
    return this.httpClient.get("myUrl");
}
//A component can be call this functions as
let data=myService.getData();
console.log(data)
//See that the call to a getHttpData is equal than the call to getObservableData
//It is the reason becaouse we can "simulate" a httpClient.get call using "of" 
myService.getObservableData().subscribe(res=>{
     console.log(res);
}
myService.getHttpData().subscribe(res=>{
     console.log(res);
}

注意2:forkJoin和map的使用

getData()
{
    let observables:Observables[];

    observables.push(of({property:"observable"});
    observables.push(of({property:"observable2"});

    return (forkJoin(observables).pipe(map(res=>{
        //in res we have [{property:"observable"},{property:"observable2"}]
        res.forEach((x,index)=>x.newProperty=i)
        //in res we have [{property:"observable",newProperty:0},
        //                {property:"observable2",newProperty:1}]
       }))
}

Update还有其他方法可以做这些事情。我认为最好有一个返回完整“问题”的功能。

//You have
jsonData:any=....
//So you can have a function that return an observable
jsonData:any=...
getJsonData()
{
   return of(this.jsonData)
}
//Well, what about to have a function thah return a fullFilled Data?
getFullFilledData()
{
   let observables:Observables[]=[];
   this.jsonData.forEach(element => {
      if (element.elementType === 'dropdown') {
         observables.push(this.http.get(element.optionsUrl))
      }
   })
   return forkJoin(observables).pipe(map(res=>
      let index = 0;
      this.jsonData.forEach((element) => {
      if (element.elementType === 'dropdown') {
         element.options = res[index];
         index++;
      }
   })
   return this.jsonData
   }))
}

这样你就不需要改变组件。如果您调用 getFullfilledData 您拥有(订阅)数据

see a 堆栈闪电战 https://stackblitz.com/edit/angular-x4a5b6-muyhat?file=src%2Fapp%2Fquestion.service.ts

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何处理服务延迟的数据? 的相关文章

随机推荐