1

I'm working through some Windows 8 tutorials from the msdn website. Specifically I'm on this one.

Part of my code (copied from the tutorial is blowing my mind why it's erroring. Sample below:

(function () {
    "use strict";

    var list = getBlogPosts();
    var groupedItems = list.createGrouped(
        function groupKeySelector(item) { return item.group.key; },
        function groupDataSelector(item) { return item.group; }
    );
    var dataPromises = [];
    var blogs;
    var blogPosts = new WinJS.Binding.List();

    function getFeeds() {
        blogs = [
            {
                key: "blog1",
                url: 'http://windowsteamblog.com/windows/b/developers/atom.aspx',
                title: 'tbd', updated: 'tbd',
                acquireSyndication: acquireSyndication, dataPromise: null
            },
            // lots more entries ...
        ];

        blogs.forEach(function (feed) {
            feed.dataPromise = feed.acquireSyndication(feed.url);
            dataPromises.push(feed.dataPromise);
        });

        return WinJS.Promise.join(dataPromises).then(function () { return blogs });
    }

// more code...

})();

At the line dataPromises.push(feed.dataPromise); I get the error JavaScript runtime error: Unable to get property 'push' of undefined or null reference. You can see dataPromises is defined and initialised to an empty array near the top of the file (I've also tried initialising it with new Array();).

What am I doing wrong here??? I'm guessing I've made some stupid screw up... Incidentally, the 3 places dataPromises appears in the snippet above are the only places it appears anywhere in the project.

My first thought was hoisting but unless something magical is going on, I'm not explicitly declaring dataPromises in any local scopes that might be overriding the top function scope.

4

1 回答 1

3

You are not following the tutorial correctly. The line

var list = getBlogPosts();

replaces the new WinJS.Binding.List() call, which occurs after the line that initializes dataPromises.

The problem is that getBlogPosts() is calling getFeeds(), and getFeeds is trying to push results onto dataPromises but the line var dataPromises = [] hasn't executed yet, so dataPromises is still undefined.

Move the call to getBlogPosts() to after the initialization of the dataPromises variable.

Stepping through the code in the debugger line by line should have exposed this problem in a fairly straightforward manner.

于 2012-08-15T21:29:19.377 回答