2013-01-06 144 views
2

我正在爲我的第一個WinJS應用程序的Windows 8,我只是想要替換2網格模板中給出的基本代碼,我越來越這個錯誤:WinJS - 無法獲取屬性'forEach'的未定義或空引用

Unable to get property 'forEach' of undefined or null reference 

我的代碼:

getData().forEach(function (item) { 
    list.push(item); 
}); 

初始功能:

function getData() { 

    // JSON request 
    WinJS.xhr({ 
     type: "GET", 
     url: "http://mytld.com/mobile/pagethatreturnsjsondata.php" 
    }).then(
      function (response) { 
       OnSuccessCall(response); 
      }, 
      function (error) { 
       var err = error; 
       console.log("Error : " + err.message); 
      }, 
      function (progress) { } 
    ); 
} 

以下正從一個PHP Web服務這是返回JSON數據的數據:

function OnSuccessCall(response) { 
    var itemContent = "<p>Item Content</p>"; 
    var itemDescription = "Item Description"; 
    var groupDescription = "Group Description"; 

    // These three strings encode placeholder images. You will want to set the 
    // backgroundImage property in your real data to be URLs to images. 
    var darkGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY3B0cPoPAANMAcOba1BlAAAAAElFTkSuQmCC"; 
    var lightGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY7h4+cp/AAhpA3h+ANDKAAAAAElFTkSuQmCC"; 
    var mediumGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY5g8dcZ/AAY/AsAlWFQ+AAAAAElFTkSuQmCC"; 

    // Each of these sample groups must have a unique key to be displayed separately. 
    var sampleGroups = [ 
     { key: "group1", title: "prayers", subtitle: "submitted prayers", backgroundImage: darkGray, description: groupDescription } 
    ]; 

    var x = response.responseText.length; 
    var json = JSON.parse(response.responseText); 
    var sampleItems = []; 
    for (var i = 0; i < json.length - 1; i++) { 
     sampleItems.push({ 
      group: sampleGroups[0], 
      title: json[i].text, 
      subtitle: "Item Subtitle: 1", 
      description: itemDescription, 
      content: itemContent, 
      backgroundImage: lightGray 
     }); 
    } 

    return sampleItems; 
} 

我能看到sampleItems數據:

watch window with object data

我明明做錯事,因爲當我將我的對象結果與原始示例數據的結果進行比較,結果幾乎相同。

任何幫助表示讚賞。

+0

您的getData函數返回null或undefined。這就是錯誤信息告訴你的。您分享了錯誤功能的代碼。 –

+0

getData調用OnSuccessCall - 爲了清晰起見,我將添加getData函數 – webdad3

回答

3

您的getData()函數沒有return語句,因此它返回undefinedundefined上不能forEach

查看大圖:您的getData()使用xhr,因此它實際上並不返回結果,而是它異步獲取數據。您因此不能forEach返回值,因爲結果不存在。您必須等待承諾才能完成,然後您可以致電承諾結果forEach

function getData() { 
    // add "return" here so the function returns a Promise 
    return WinJS.xhr({ 
     type: "GET", 
     url: "http://mytld.com/mobile/pagethatreturnsjsondata.php" 
    }).then(
      function (response) { 
       // add "return" here to specify the promise's completed value 
       return OnSuccessCall(response); 
      }, 
      function (error) { 
       var err = error; 
       console.log("Error : " + err.message); 
      }, 
      function (progress) { } 
    ); 
} 

現在getData()回報的東西(許諾)你可以安排工作的承諾完成時發生。

getData().then(function(results) { results.forEach(...) }); 
相關問題