Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieving object and iterating through it

    I'm working on a chat project, and I'm trying to retrieve the messages from the server and get them into a form where I can use them. I'm fairly new to Javascript but very new to AJAX and interacting with a server, so please bear with me on this.

    Right now, here is what I have to fetch the messages:

    Code:
    function fetch(func) {
              var messageObj = $.ajax({
                type: "GET",
                url: "https://api.parse.com/1/classes/chats?order=-createdAt",
              });
              console.log(messageObj)
              console.log(Object.keys(messageObj));
              func(messageObj);
            }
    As you can see, it's pulling in an object, and I'm printing out it and its keys to the log. I've attached a picture of the log. When logging the object itself, it's showing a responseText property (I think?) that has the info I need. The rest I don't. But when I log the keys of the messageObj, responseText isn't on there. I just can't figure out how to access that! Any help would be appreciated... Thanks!
    Attached Thumbnails Attached Thumbnails Retrieving object and iterating through it-log.jpg  

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    function fetch(func) {
              var messageObj;
                 $.ajax({
                type: "GET",
                url: "https://api.parse.com/1/classes/chats?order=-createdAt",
               success: function(data){
               messageObj = data;
              }
              });
              console.log(messageObj)
              console.log(Object.keys(messageObj));
              func(messageObj);
            }

  • #3
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the quick response. Unfortunately, when I do that, messageObj becomes undefined and logging the keys produces an error. I guess all I really need is a way to get the responseText out of the object I had initially, is there a special way to do that? I've tried messageObj.responseText, but that just gives me undefined, even though it shows up when I log messageObj.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,366
    Thanks
    11
    Thanked 590 Times in 571 Posts
    Quote Originally Posted by krm42x View Post
    Thanks for the quick response. Unfortunately, when I do that, messageObj becomes undefined and logging the keys produces an error. I guess all I really need is a way to get the responseText out of the object I had initially, is there a special way to do that? I've tried messageObj.responseText, but that just gives me undefined, even though it shows up when I log messageObj.

    you actually want this:

    Code:
    function fetch(func) {
              var messageObj;
              $.ajax({
                  type: "GET",
                 url: "https://api.parse.com/1/classes/chats?order=-createdAt",
                 success: function(messageObj ){
    
                   console.log(messageObj)
                   console.log(Object.keys(messageObj));
                   console.log(JSON.parse(messageObj.responseText||0)); // bonus answer for your next question...
                   func(messageObj);
    
                 }
              });
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That returned an object with an array of objects inside... but I was able to get what I needed out of it! Thanks! If you don't mind, could you explain why that works differently than what I had before? Is it because the messageObj comes from the success function instead of just being defined like I had it?

    I also don't understand the line with JSON in it... it just logs 0. Is that what was supposed to happen? Thanks!

  • #6
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So everything seems to be working as expected, but for some reason it's still reporting an error. Here's the func from above that receives the messageArray (no longer messageObj, just an array with the latest messages):

    Code:
    function splitter (arr) {
              console.log(arr.length);
              for (var i = 0; i < arr.length; i++) {
                display(arr[i], ".messages");
              }
            }
    I get an error on the second line there: "Uncaught TypeError: Cannot read property 'length' of undefined." But the log gives me the correct length. Why would it log it correctly and then have an error? It also comes up with an error on the third line for the same reason, but only if I comment out line 2. Again, everything works as it should, but it's reporting that error. Thanks for any help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •