简体   繁体   中英

“Uncaught TypeError: Cannot read property 'x' of undefined” when trying to get a property from an object

So, I get my list of Steam games in JSON through the Steam Web API. When I try to get a property from the JSON directly through the Chrome console, it's all good. But when I execute the same line in my code, it throws Uncaught TypeError: Cannot read property 'x' of undefined ('x' stands for whatever property I try to get).

key = "lotsofrandomnumbersandletters";
id = "steamprofileid";
var gameList = $.getJSON("http://www.corsproxy.com/api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" + key + "&steamid=" + id + "&format=json");
var gameCount = gameList.responseJSON.response.game_count;

$.getJSON() is asynchronous and you need to access its result in a callback...

$.getJSON("http://www.corsproxy.com/api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" 
        + key + "&steamid=" + id + "&format=json", function (gameList) {
    var gameCount = gameList.responseJSON.response.game_count;
});

使用它安东尼的答案,但在宣布解决gameCount之外$.getJSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM