JQuery 延期,对 SharePoint 列表的承诺

JQuery Deferred, Promises with SharePoint List

我正在运行使用以下代码获取 SharePoint 列表项,使用 JQuery Deferred/Promises 对该列表执行某些操作。但是当我 运行 代码时,我得到了错误:

'The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.'

请找到下面的代码:

$(document).ready(function () {
    //Do not execute jsom until sp.js file has loaded.
    ExecuteOrDelayUntilScriptLoaded(createGamesCloudDashboard, "sp.js");});
    //Function to retrieve Games Cloud Order Book from SharePoint server.
    function createGamesCloudDashboard() {
       getGamesCloudOrderBook().then(
         outputGamesCloudRevenue(gamesCloudItems),     
         function (sender, args) { 
           console.log('An error occurred while retrieving Games Cloud Order Books.')
         });
    }

   //CAML Query definition.
   function getGamesCloudOrderBook() {
       // Create Deferred object to run the consumer of 
       // the Games Cloud Order Book synchronously.
       var deferred = $.Deferred();
       var clContext = new SP.ClientContext('some url');
       var spList = clContext.get_web().get_lists().getByTitle('Name of List');   
       var gamesCloudOrderBook = new SP.CamlQuery();
       this.gamesCloudItems = spList.getItems(gamesCloudOrderBook);
       clContext.load(gamesCloudItems);
       clContext.executeQueryAsync(
         Function.createDelegate(this, 
           function () { deferred.resolve(gamesCloudItems) }),
         Function.createDelegate(this, 
           function (sender, args) { deferred.reject(sender, args) }));

       return deferred.promise();
   }

   function outputGamesCloudRevenue(gamesCloudItems) {
       //do something with the list.
   }

.then() 需要命名或匿名函数委托作为参数。

在下面的一段代码中...

getGamesCloudOrderBook().then(
     outputGamesCloudRevenue(gamesCloudItems),     
     function (sender, args) { 
       ...        
    });

...你没有传入函数 outputGamesCloudRevenue 作为第一个参数,你实际上是在执行函数(使用未定义的变量 gamesCloudItems 作为其参数)然后传递执行函数的 return 值(如果有)到 .then().

试试这个:

 getGamesCloudOrderBook().then(
     outputGamesCloudRevenue,     
     function (sender, args) { 
       console.log('An error occurred while retrieving Games Cloud Order Books.')
     });