【问题标题】:AngularJS Foreach not waitingAngularJS Foreach 不等待
【发布时间】:2018-04-17 01:23:30
【问题描述】:

我正在使用 angularjs 尝试将多个文件上传到我的共享点 API,所以我做了什么我使用带有 $q.all 的 foreach 语句来等待所有内容完成上传,但不知何故,我的代码在第一次上传时已经被认为已解决文件。

factory.UploadFilesByBatch = function(arrayBuffer, folderName) { var 承诺 = [];

    angular.forEach(arrayBuffer, function(item) {
        // factory.GetFormDigest().then(function(data) {
        var deferred = $q.defer();
        // promises.push(deferred);

        console.log("UploadFilesByBatch Try - " + item.fileName + "Uploading")
        $http({
            url: webUrl + "web/getfolderbyserverrelativeurl('" + folderLocation + "')/files/add(overwrite=true, url='" + item.fileName + "')",
            method: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": xrequestDigest,
                "content-Type": "application/json;odata=verbose"
            },
            data: item.fileBuffer,
            transformRequest: []
        }).then(function(result) {
            console.log("UploadFilesByBatch Success - " + item.fileName + "Uploaded")
            item.sharepointFileLocation = result.data.d.ServerRelativeUrl;
            deferred.resolve(item);

        }, function(result, status) {
            console.log("UploadFilesByBatch Error - " + item.fileName + "Failed")
            console.log(JSON.stringify(result))
            deferred.reject(result);
        });
        promises.push(deferred);

        // })

    })
    return $q.all(promises);

};

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您没有完全正确地使用 $q.all。您想将实际的承诺推入您的承诺数组中。您还希望摆脱所有延迟的东西,$http 调用已经是一个承诺,因此您可以将它直接推送到您的承诺数组中。

    你想做的不是promises.push(deferred);

    promises.push($http({
                    url: webUrl + "web/getfolderbyserverrelativeurl('" + folderLocation + "')/files/add(overwrite=true, url='" + item.fileName + "')",
                    method: "POST",
                    headers: {
                        "Accept": "application/json;odata=verbose",
                        "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue,
                        "content-Type": "application/json;odata=verbose"
                    },
                    data: item.fileBuffer,
                    transformRequest: []
                })
    );
    

    这会给你留下一系列的承诺。然后您可以使用$q.all(promises) 来迭代结果,如下所示:

    $q.all(promises).then(function(promiseResults) {
      // promiseResult is an array of the return values from the $http request in the order they were pushed into the promises array
      angular.forEach(promiseResults, function(result, resultIndex) {
        // Whatever you wanted to do to each result here
      });
    }
    

    【讨论】:

    • 感谢您的回复,它解决了这个问题。现在,如果我想在返回时返回一个不同的对象怎么办?正如您在每次上传后看到的那样,我正在尝试修改我想返回的“arrayBuffer”的属性。
    • 哎呀,nvm,我想我解决了 :) 再次感谢您的帮助,尽力而为
    • @SharePointLovesMe 如果你得到答案,请接受它
    • @SharePointLovesMe 很高兴我能帮忙!
    猜你喜欢
    • 2019-06-29
    • 2016-05-08
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多