如何等待,循环的下一个迭代以从Angular中获得API的响应?

发布于 2025-02-02 23:22:39 字数 1189 浏览 1 评论 0原文

我正在学习角度。 我在循环中调用功能。该功能正在调用API请求并从服务器中获得一些响应。我正在使用一些数据上传多个文件。

在调用API时,请求其随机文件与FormData中的文件数组中的数据一起获取随机文件。

问题在于,数据是根据文件的特定索引上传到第一次迭代中的第一个文件,在第二个迭代中,应该有第二个文件,但是FormData是从数组中获取下一个或有时是先前的文件文件。

我想等到获得API的响应,然后处理下一个迭代。如何实现? 我的代码如下: 浏览多个文件后,请在按钮上单击“ generate”函数。 变量 - :this.filestoupload包含多个文件。

generate() {
    for(var i = 0; i<this.data.length; i++){
        var name= this.data[i];
        if(this.data.length != 10){
            this.alertService.danger('Name');
            return false;
        }else{
            this.uploadData2(name, i)
        }          
    }
}

uploadData2(myname, loopIndex) {
   var body = {number:"1234"}

   this.http.post(URL, body)
            .subscribe((data: any) => {
              uploadData3(myname, loopIndex);
            });

}

uploadData3(myname, loopIndex) {
   var formData = new FormData();
   const files: Array<File> = this.filesToUpload;
   formData.append("uploads[]", files[loopIndex], files[loopIndex]['name']);
   formData.append("myname", myname);

   this.http.post(URL, formData)
            .subscribe((data: any) => {
              //ressponse here
            });

}

I am learning angular.
I am calling a function in a for loop. The function is calling an API request and getting some response from server. I am uploading multiple file with some data.

While calling the API request its taking the random file with the data from the files Array in formData.

The problem is that the data is uploading according to the particular index of file like in first iteration there should be first file with the data and in second iteration there should be 2nd file, but the formData is taking next or sometimes previous file from the Array of files.

I want to wait until to get the response from API then processed the next iteration. How to achieve that?
My Code is below:
The generate() function is called on button click after browsing multiple files.
and the variable -: this.filesToUpload contains multiple files.

generate() {
    for(var i = 0; i<this.data.length; i++){
        var name= this.data[i];
        if(this.data.length != 10){
            this.alertService.danger('Name');
            return false;
        }else{
            this.uploadData2(name, i)
        }          
    }
}

uploadData2(myname, loopIndex) {
   var body = {number:"1234"}

   this.http.post(URL, body)
            .subscribe((data: any) => {
              uploadData3(myname, loopIndex);
            });

}

uploadData3(myname, loopIndex) {
   var formData = new FormData();
   const files: Array<File> = this.filesToUpload;
   formData.append("uploads[]", files[loopIndex], files[loopIndex]['name']);
   formData.append("myname", myname);

   this.http.post(URL, formData)
            .subscribe((data: any) => {
              //ressponse here
            });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-02-09 23:22:39

如评论中的建议;请花一些时间学习RXJ。当您像代码中一样使用异步调用(和功能)时,这将为您提供帮助。

解决拼图的一种方法是,在服务器上生成一个带有httpcalls的可观察数组阵列的顺序)。

它需要一些时间来学习,但是您不会后悔!

generate() {
    let obsArray = Observable<any>[]=[];
    for(var i = 0; i<this.data.length; i++){
        var name= this.data[i];
            obsArray.push(this.uploadData2(name, i))          
    }
    concat(obsArray).subscribe(res=>console.log("After merge:",res))
}

uploadData2(myname, loopIndex) {
   var body = {number:"1234"}

   return this.http.post(URL, body)
      .pipe()
            mergeMap(data => 
              uploadData3(myname, loopIndex);
            );

}

uploadData3(myname, loopIndex) {
   var formData = new FormData();
   const files: Array<File> = this.filesToUpload;
   formData.append("uploads[]", files[loopIndex], files[loopIndex]['name']);
   formData.append("myname", myname);

   return this.http.post(URL, formData)
        .pipe(tap(... some side effects here...))

}

该代码未进行测试,但应该让您了解其应该如何外观。

As suggested in the comments; please invest some time to learn RxJS. This will help you when you're working with asynchronous calls (and functions) like in your code.

One way of solving your puzzle is to generate an Observable Array with httpCalls to the server and then subscribe to them (fi with merge() if you want to execute the calls in parallel or with concat() if you want to process them in the order of the array).

It will take some time to learn, but you will not regret that!.

generate() {
    let obsArray = Observable<any>[]=[];
    for(var i = 0; i<this.data.length; i++){
        var name= this.data[i];
            obsArray.push(this.uploadData2(name, i))          
    }
    concat(obsArray).subscribe(res=>console.log("After merge:",res))
}

uploadData2(myname, loopIndex) {
   var body = {number:"1234"}

   return this.http.post(URL, body)
      .pipe()
            mergeMap(data => 
              uploadData3(myname, loopIndex);
            );

}

uploadData3(myname, loopIndex) {
   var formData = new FormData();
   const files: Array<File> = this.filesToUpload;
   formData.append("uploads[]", files[loopIndex], files[loopIndex]['name']);
   formData.append("myname", myname);

   return this.http.post(URL, formData)
        .pipe(tap(... some side effects here...))

}

The code is not tested, but should give you an idea on how it should look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文