简体   繁体   English

数组给我“无法读取未定义的属性”,但console.logs显示其他信息

[英]Array gives me 'Cannot read property of undefined' but console.logs show otherwhise

I'm trying to do a method in which i process an array of messages, every message haves an id of the user sending the message and the content of the meesage itself. 我正在尝试执行一种方法,在该方法中我处理一系列消息,每个消息都有发送消息的用户的ID和消息本身的内容。

Once i get the array, i try to separate the messages by the user who send it (In order to style the messages according to the users later on). 一旦获得数组,我将尝试按发送该数组的用户分开消息(以便稍后根据用户设置消息样式)。

I attempt to do this with a for loop to read the array, and an if to check if the logged user send such message or not. 我尝试使用for循环来执行此操作,以读取该数组,并使用if来检查登录用户是否发送了此类消息。

The problem pops up when i want to read who send the message. 当我想阅读发送消息的人时,问题弹出。

At first i thought "i may be writing the sentence wrong", so i used some consol.logs to see what was wrong. 起初我以为“我可能把句子写错了”,所以我使用了一些consol.logs来查看出了什么问题。 And i got an interesting situation going on: The variable shows correctly on the console, but i get an error once i want to evaluate on the if . 而且我遇到了一个有趣的情况:变量在控制台上正确显示,但是一旦我想对if求值,我就会报错。

Here's the JavaScript code i'm running 这是我正在运行的JavaScript代码

function processMeesages(allMessages) {

  for (var i = 0; i <= allMessages.length; i++) {

    console.log('allMessages[' + i + '].userMsg', allMessages[i].userMsg)

    //Message from the logged user
    if (allMessages[i].userMsg == user.id) {
      chatMessages += '<div class="row ">' +
              '            <div class="col col-10 "></div>' +
              '            <div class="col card owner-container">' +
              '                <div class="item item-text-wrap owner">' +
                                  allMessages[i].contenido +
              '                </div>' +
              '            </div>' +
              '        </div>';
    }

    //Messages from the other user
    else {
      chatMessages += '<div class="row ">' +
              '            <div class="col card message-container">' +
              '                <div class="item item-text-wrap message">' +
                                  allMessages[i].contenido +
              '                </div>' +
              '            </div>' +
              '            <div class="col col-10 "></div>' +
              '        </div>';
    }
  }

  //Once the for loop ends (What's down didn't show up on the console)
  $rootScope.chatMessages = chatMessages;
  console.log('chatMessages', chatMessages)
  console.log('rootscopeChatMessages', $rootScope.chatMessages)
}

Here's an image with what i get in the console and what the array haves inside (I put some labels on the picture) 这是一张图像,其中包含我在控制台中获得的内容以及数组内部所包含的内容(我在图片上贴了一些标签)

Seems like you are looping more than required. 好像您循环的次数超过了要求。 Your array only has two elements but your for loop tries to loop over 3 times, hence giving that access error because allMessages[2] is undefined. 您的数组只有两个元素,但是您的for循环尝试循环3次以上,因此由于allMessages[2]未定义,因此会出现该访问错误。

function processMeesages(allMessages) {

  for (var i = 0; i < allMessages.length; i++) {

    console.log('allMessages[' + i + '].userMsg', allMessages[i].userMsg)

    .
    .
    .
    .
}

暂无
暂无

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

相关问题 在 Google Chrome 中看不到 console.logs - Cannot see console.logs in Google Chrome 访问 object 中的数组长度给我“TypeError:无法读取未定义的属性“长度” - accessing length of an array inside an object gives me 'TypeError: Cannot read property 'length' of undefined' Browserify 给了我未捕获的类型错误:无法读取未定义的属性“原型” - Browserify gives me Uncaught TypeError: Cannot read property 'prototype' of undefined function 给我 TypeError: Cannot read property 'resize' of undefined - function gives me TypeError: Cannot read property 'resize' of undefined 在 ReactJS 中设置 state 给我:无法读取未定义的属性“setState” - Setting a state in ReactJS gives me: Cannot read property 'setState' of undefined 为什么这个 function 返回 undefined 但 console.logs 正确答案? - Why does this function return undefined but console.logs the correct answer? 角度推入数组给出:TypeError:无法读取未定义的属性“ push” - Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined 删除了库中的 console.logs 仍然显示在客户端 - Removed console.logs in library still show on the client 传递数组不起作用使我在控制台中未定义 - Passing an array infunction gives me undefined in console 无法理解为什么它给了我:未捕获(承诺)类型错误:无法读取未定义的属性“blabla” - Unable to understand why it gives me: Uncaught (in promise) TypeError: Cannot read property 'blabla' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM