Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting undefined when trying to acces a JSON item
    text
    copied!<p>i am trying to get a JSON and access its objects to retrieve data, all my paths seems to be working good but one specifically is not working. I just checked and the path is the same the other objects i am trying to access, in the console i keep getting undefined as a result. i will appreciate if you guys could tell me whats wrong with the code i will leave you the JSON structure and also the java Scripti hope you guys can help me figuring out, and also would be good if you could give me some tips on my coding because I am learning.</p> <p>Java Script: This function is called when you click on some item in a list.</p> <pre><code>function checkUser(){ console.log("clicked"); var user = $(this).html(); $.getJSON("js/options.json", function(json){ var itemsLength = json.chat.OnlineContacts.length; for ( var i = 0; i &lt; itemsLength; i++) { var jsonUserName = json.chat.OnlineContacts[i].name; var jsonUserStatus = json.chat.OnlineContacts[i].status; var jsonUserAvatar = json.chat.OnlineContacts[i].avatar; if(user == jsonUserName){ /*displayChatWindow(jsonUserName, jsonUserStatus, jsonUserAvatar);*/ console.log(jsonUserAvatar); } }; }); } function displayChatWindow(user, status, avatar){ /*var template = _.template($("#windowTemplate").html(), {userName: user, userStatus: status, userAvatar: avatar}); $("body").prepend(template);*/ $(".messages-container").slimScroll({ height: '200', size: '10px', position: 'right', color: '#535a61', alwaysVisible: false, distance: '0', railVisible: true, railColor: '#222', railOpacity: 0.3, wheelStep: 10, disableFadeOut: false, start: "bottom" }); } </code></pre> <p>And this is the JSON:</p> <pre><code>{ "chat": { "NumberOfOnlineContacts": "7", "NumberOfOfflineContacts": "800", "OnlineContacts": [ { "name": "Nandy Torres", "status": "online", "avatar": "img/profile-picture2.jpg" }, { "name": "Catherine Varela", "status": "Busy", "avatar": "img/profile-picture3.jpg" }, { "name": "Jhonnatan Gonzalez", "status": "online", "avatar": "img/profile-picture4.jpg" }, { "name": "Juan Prieto", "status": "away", "avatar": "img/profile-picture5.jpg" }, { "name": "Alexander Barranco", "status": "Busy", "avatar": "img/profile-picture6.jpg" } ], "OfflineContacts": [ { "name": "Nandy Torres" }, { "name": "Catherine Varela" }, { "name": "Jhonnatan Gonzalez" }, { "name": "Juan Prieto" }, { "name": "Jhonathan Sanchez" } ] } } </code></pre> <p>When I try to do the console log on avatar like is in the example I just get undefined, but if i do console.log of name or status y just get the correct values.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload