2

我正在为 Windows 8 开发我的第一个 WinJS 应用程序,我只是想替换 2 网格模板中给出的基本代码,我收到了这个错误:

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) { }
    );
}

以下是从返回 JSON 数据的 php webservice 获取数据:

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 中看到数据:

带有对象数据的监视窗口

我显然做错了什么,因为当我将我的对象结果与原始示例数据的结果进行比较时,它们实际上是相同的。

任何帮助表示赞赏。

4

1 回答 1

3

您的getData()函数没有return语句,因此它返回undefined. 而且你不能forEachundefined

纵观全局:您的getData()使用xhr,因此它实际上并没有返回结果,而是异步获取数据。因此,您不能forEach返回值,因为结果还不存在。您必须等待 promise 完成,然后才能调用forEachpromise 结果。

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()返回了一些东西(一个 Promise),你可以安排工作在 Promise 完成时发生。

getData().then(function(results) { results.forEach(...) });
于 2013-01-06T14:54:39.597 回答