简体   繁体   English

循环超出范围?

[英]For loop going out of range?

I am using a library in Javascript to pull in data from a chat server. 我正在使用Javascript库从聊天服务器提取数据。 The library is irrelevant but is quickblox. 该库无关紧要,但是它是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. 数据返回到我的应用程序中并且可以看到,但是当我尝试对对象返回的循环时,数据超出范围,无法获取未定义的“ last_message”。 Loop should run for res.items[i].length which at the mo is two, but it is trying to carry on running, it seems. 循环应该在res.items [i] .length上运行,这在mo处是2,但是它似乎试图继续运行。

        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
for (i;i<=res.items.length;i++){

应该

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

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++){
        ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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