-1

I am using a library in Javascript to pull in data from a chat server. The library is irrelevant but is quickblox. The data comes back into my app and can be seen, but when I try an loop on an object they return, it goes out of range, can't get 'last_message' of undefined. Loop should run for res.items[i].length which at the mo is two, but it is trying to carry on running, it seems.

        var onDialogs = function(err, res){
            console.log("------------------------------------List Of Dialogs------------------------------------",res);
            var count = 0;
            var sent;

            var i = 0;
                console.log("res.items.length",res.items.length)
                for (i;i<=res.items.length;i++){
                    console.log("this one: ", res.items[i]);
                    if (res.items[i].last_message===null||res.items[i].last_message===undefined||res.items[i].last_message===""){
                        alert("SOMETHING WENT WRONG");
                    }
                else{
                    console.log("RES.ITEMS: ", res.items);
                console.log("RES ITEMS LEN", res.items.length);
                console.log("***********************I IS SET TO: ",i," *******************************************");
                console.log("RAWR",res.items);
                console.log(res.items[i].last_message_date_sent,res.items[i].last_message);
                console.log(res.items[i]);

                if (res.items[i].last_message === undefined || res.items[i].last_message===null || res.items[i].last_message===""){
                    console.log("FAIL");
                }
                else{
                    var dialogID = res.items[i]._id;
                    var sent = res.items[i].created_at;
                    console.log(res.items[i].created_at);
                    var year = sent.substring(0,4);
                    var month = sent.substring(5,7);
                    var day = sent.substring(8,10);
                    var userIDInChat;
                    var j =0;
                    userArray=[];

                    var userArray = res.items[i].occupants_ids;
                    console.log("USER ARRAY: ",userArray);
                    for (j; j<userArray.length; j++){
                        console.log(userArray[j]);
                        var testID = window.localStorage.getItem("userID");
                        console.log("USERARRAY[j]", userArray[j]);
                        if (userArray[j] != testID){
                            console.log("INSIDE THE IF STATEMENT");
                            userIDInChat = userArray[j];
                            window.localStorage.setItem("userIDInChat", userIDInChat);
                            console.log("//*******BLOCK ID SET TO********\\", userIDInChat, testID, res);
                            $.get("http://www.domain.co.uk/API/getUserByID.php", { userID: userIDInChat}, function (data) {
                                console.log("API CALL:",data);
                                chatOpponent = data;
                                console.log(chatOpponent);
                                console.log("------------------------------------------------------------------------------------------");
                                renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID);
                            });
                        }
                    }
                }
            }
        }
   //End block
        };


        function renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID){
                console.log("(res,j,i,chatOpponent,userIDInChat,userArray,testID)");
                console.log("RENDERBLOCK PARAMS: ",res,j,i,chatOpponent,userIDInChat,userArray,testID);
                //insert function here
                console.log("RES: ",res);
                var senderID = userIDInChat;
                //compare date - vs. moment - today, yesterday or date
                sent = day + "/" + month + "/" + year;
                console.log(sent);
                var onMessages = function(err,result){
                    window.localStorage.setItem("RESULTTEST",result);
                    console.log("ONMESSAGESRESULTHERE",err,result);
                    //console.log("---------onMessages---------",result.items[i].date_sent);s
                };
                var msgList = QB.chat.message.list({chat_dialog_id: dialogID}, onMessages);
                var messages;
                console.log(messages);
                if (res.items[i].last_message.length>=140) {
                    var last_message = res.items[i].last_message.substring(0,140)+".....";
                }
                else{
                    var last_message = res.items[i].last_message;
                }
                var readFlag = res.items[i].read;
                console.log("SENDERID:", senderID, "username: ", chatOpponent, "last_message", last_message, "sentFlag");
                if (readFlag === 1){
                    var read = "fa-envelope-o";
                }
                else {
                    var read = "fa-envelope";
                }
                var html = "<div class='messageBlock' id='"+senderID+"'><div style='width:10%;min-height:64px;float:left;'><i class='fa '"+read+"'></i><p>"+sent+"</p></div><div style='width:90%;min-height:64px;float:right;'><p class='user'><b><i>"+chatOpponent+"</b></i></p><p>"+last_message+"</p></div></div>";

Object being looped on:

Object {total_entries: 2, skip: 0, limit: 50, items: Array[2]}items: Array[2]0: Object_id: "54e4bd3929108282d4072a37"created_at: "2015-02-18T16:26:33Z"last_message: "test"last_message_date_sent: 1425640757last_message_user_id: 2351789name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351781xmpp_room_jid: null__proto__: Object1: Object_id: "54ec990f29108282d40b19e2"created_at: "2015-02-24T15:30:23Z"last_message: "herro!"last_message_date_sent: 1424858692last_message_user_id: 2394026name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351789xmpp_room_jid: null__proto__: Objectlength: 2__proto__: Array[0]limit: 50skip: 0total_entries: 2__proto__: Object
anthonyhumphreys
  • 1,051
  • 2
  • 12
  • 25
  • 4
    Are we supposed to trawl through all those lines of html/css/js to find the one line you're asking about? – Jamiec Mar 06 '15 at 14:55
  • 1
    Only post code relevant to your question – charlietfl Mar 06 '15 at 15:00
  • Sorry, I hadn't meant to post so much code! My bad. I have trimmed it - i know there is still quite a lot of code there, but I'm not too sure whether the problem is seeded earlier than anticipated. the line of code in the answer below was problematic but the problem still occurs after correcting the code. Can you spot why this might be? Is it because of the way the for loop has been nested? – anthonyhumphreys Mar 07 '15 at 15:18

2 Answers2

3
for (i;i<=res.items.length;i++){

should be

for (i;i<res.items.length;i++){
1

Simple you are looping one too many times.

for (i;i<=res.items.length;i++){
        ^^

arrays are zero index so that means that last index is the length minus one.

for (i;i<res.items.length;i++){
        ^^
epascarello
  • 204,599
  • 20
  • 195
  • 236