获取错误“无法读取未定义的属性(reading' Sender;)’一旦我比操作员等于或少

发布于 2025-02-14 00:12:16 字数 778 浏览 0 评论 0原文


export const isSameSender = (
  messages: any[],
  message: any,
  index: number,
  user: IUser
) => {
  return (
   //if I put just "<" instead of "<=" in the below line, there's no error
    index <= messages.length - 1 &&
    messages[index + 1].sender._id === message.sender._id && //this line gives that error
    messages[index].sender._id !== user._id
  )
}

export const isLastMessage = (messages: any[], index: number, user: IUser) => {
  return index <= messages.length - 1 && messages[index].sender._id !== user._id
}

我觉得如果我无法理解&amp;正确,但是为什么推杆&lt; =而不是&lt;给出错误,我试图检查消息数组是否已达到最终索引,以检查它是否已完成循环,但是一旦我使用&lt; =在此代码中,第二个代码就会给出错误,我不知道为什么为什么。

谁能启发我发生的事情?如果给出任何[]作为消息数组的类型正在引起此问题?


export const isSameSender = (
  messages: any[],
  message: any,
  index: number,
  user: IUser
) => {
  return (
   //if I put just "<" instead of "<=" in the below line, there's no error
    index <= messages.length - 1 &&
    messages[index + 1].sender._id === message.sender._id && //this line gives that error
    messages[index].sender._id !== user._id
  )
}

export const isLastMessage = (messages: any[], index: number, user: IUser) => {
  return index <= messages.length - 1 && messages[index].sender._id !== user._id
}

I feel if I haven't been able to understand && properly but why does puttting <= instead of < gives the error, I am trying to check if the messages array has reached it's final index to check if it has finished looping but as soon as I use <= in this code the second code gives the error, I don't know why.

Can anyone enlighten me on what's happening? If giving any[] as a type to the messages array is causing this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-02-21 00:12:16

这是因为如果index实际上等于message.length -1,则

消息[index + 1]是不确定的messages.length -1 + 1] ,意思是消息[messages.length],因此undefined,因为Message> Message> Message /代码>数组。

It's because if index is actually equal to messages.length - 1, then

messages[index + 1] is undefined because messages[messages.length - 1 + 1] which means messages[messages.length], so undefined because there is not such index in the messages array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文