2016-03-10 39 views
0

我正在學習Angular,並在此過程中爲SharePoint數據創建服務。我現在有一個更新SharePoint項目的功能,但我遇到了問題。如果函數被調用並在嘗試更新項目時遇到錯誤,則所有項目都會完成更新,但只會調用第一個拒絕(失敗項目)。如果任何其他項目失敗,則不會爲他們調用拒絕。我希望能夠將生成的所有錯誤推送到數組中,以便我們可以將其傳回給用戶,以便他們可以處理所有錯誤。以下是我的代碼:

this.UpdateListItems = function (webUrl, listName, itemsJson) { 
    var deferred = $q.defer(); 
    var promises = []; 

    angular.forEach(itemsJson, function (itemProps) { 
     var itemPromise = this.UpdateListItem(webUrl, listName, itemProps.Id, itemProps) 
      .then(function (response) { 
       return deferred.resolve(response); 
      }, function (error) { 
       return deferred.reject(error); 
      }); 
     promises.push(itemPromise) 
    }, this); 

    return $q.all(promises) 
     .then(function (data) { 
      return lists[listName]; 
     }, function() { 
      console.info("Promises: " + JSON.stringify(promises)); 
      return $q.reject(promises); 
     }); 
}; 

此處還有返回的JSON承諾。您將看到4個承諾對象返回,其中前2個被掛起(他們成功),然後第三個失敗,因爲該項目不存在於服務器上。最後,第四還應該失敗出於同樣的原因爲第三,但你可以看到立即拒絕返回時的第一個失敗,不會處理任何其他不合格的項目:

[ 
    { 
    "$$state": { 
     "status": 0, 
     "pending": [ 
     [ 
      { 
      "promise": { 
       "$$state": { 
       "status": 0 
       } 
      } 
      }, 
      null, 
      null, 
      null 
     ] 
     ] 
    } 
    }, 
    { 
    "$$state": { 
     "status": 0, 
     "pending": [ 
     [ 
      { 
      "promise": { 
       "$$state": { 
       "status": 0 
       } 
      } 
      }, 
      null, 
      null, 
      null 
     ] 
     ] 
    } 
    }, 
    { 
    "$$state": { 
     "status": 2, 
     "value": { 
     "data": { 
      "error": { 
      "code": "", 
      "message": { 
       "lang": "en-US", 
       "value": "Resource not found for the segment 'TestList'." 
      } 
      } 
     }, 
     "status": 404, 
     "config": { 
      "method": "GET", 
      "transformRequest": [ 
      null 
      ], 
      "transformResponse": [ 
      null 
      ], 
      "url": "http://kl-sp10dev04/sites/angular/_vti_bin/listdata.svc/TestList(126)", 
      "processData": false, 
      "headers": { 
      "Accept": "application/json;odata=verbose" 
      } 
     }, 
     "statusText": "Not Found" 
     }, 
     "processScheduled": false 
    } 
    }, 
    { 
    "$$state": { 
     "status": 0, 
     "pending": [ 
     [ 
      { 
      "promise": { 
       "$$state": { 
       "status": 0 
       } 
      } 
      }, 
      null, 
      null, 
      null 
     ] 
     ] 
    } 
    } 
] 
+0

我相信你有$ q.defer(),然後再您可以將解析或拒絕添加到$ q中。 –

+0

你能編輯我的代碼來告訴我你的意思嗎?我試圖做到這一點,但迄今爲止它不工作。謝謝。 –

+0

我對您的代碼進行了編輯,但是我可以看到您已經得到了答案。你的代碼很混亂,因爲你在UpdateListItems中調用UpdateListItems,看起來像遞歸,但事實並非如此。我很困惑你的代碼應該做什麼。 –

回答

0

對於誰運行到任何人這個問題在未來這裏就是答案。這是我現在使用的代碼,它按預期工作。這是它返回一個承諾,等待所有項目承諾完成。如果所有項目都成功更新,則通過返回對JSON對象列表的引用來解決此問題。如果任何項目未能更新,則拒絕將所有錯誤的數組返回給用戶進行錯誤處理。請注意,Lists數組是該服務的一個屬性,因此您在此處未看到它的定義。

this.UpdateListItems = function (webUrl, listName, itemsJson) { 
    var promises = []; 
    var errors = []; 
    angular.forEach(itemsJson, function (itemProps) { 
     promises.push(this.UpdateListItem(webUrl, listName, itemProps.Id, itemProps) 
      .then(function (response) { 
       return itemsJson[listName]; 
      }, function (error) { 
       errors.push(error); 
      })); 
    }, this); 
    return $q.all(promises) 
     .then(function() { 
      if (errors.length > 0) { 
       return $q.reject(errors); 
      } 
      else { 
       return lists[listName] 
      } 
     }); 
}; 
0

它是由社會各界要求允許$ q.all()繼續一個失敗後,但它不是由角來實現。有些人寫了一些$q臨時演員,如this one

複製/粘貼後,您將需要引用'angular-q-extras'模塊在你的代碼,並呼籲

$q.allSettled(promises).then(results => ...//some fails, some not) 

現在你可以CA