获取错误“无法读取未定义的属性(reading' Sender;)’一旦我比操作员等于或少
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为如果
index
实际上等于message.length -1
,则消息[index + 1]
是不确定的messages.length -1 + 1] ,意思是消息[messages.length]
,因此undefined
,因为Message> Message> Message
/代码>数组。It's because if
index
is actually equal tomessages.length - 1
, thenmessages[index + 1]
is undefined becausemessages[messages.length - 1 + 1]
which meansmessages[messages.length]
, soundefined
because there is not such index in themessages
array.