我在Javascript中使用库来从聊天服务器中提取数据。图书馆无关紧要,但却是quickblox。数据会回到我的应用程序中并且可以看到,但是当我在他们返回的对象上尝试循环时,它会超出范围,无法获得' last_message'未定义的。循环应该运行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 {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
答案 0 :(得分:3)
for (i;i<=res.items.length;i++){
应该是
for (i;i<res.items.length;i++){
答案 1 :(得分:1)
很简单,你循环太多次了。
for (i;i<=res.items.length;i++){
^^
数组是零索引,这意味着最后一个索引是长度减一。
for (i;i<res.items.length;i++){
^^