Angular 错误类型错误:无法读取属性';未读';未定义的
当页面第一次加载时,我会不断收到下面的错误,但我可以退出页面,一切都会按预期进行Angular 错误类型错误:无法读取属性';未读';未定义的,angular,typescript,observable,ionic3,angularfire2,Angular,Typescript,Observable,Ionic3,Angularfire2,当页面第一次加载时,我会不断收到下面的错误,但我可以退出页面,一切都会按预期进行 ERROR TypeError: Cannot read property 'unread' of undefined at listing-element.ts:34 at Array.forEach (<anonymous>) at SafeSubscriber._next (listing-element.ts:33) at SafeSubscriber.__tryOrUnsub (Subscr
ERROR TypeError: Cannot read property 'unread'
of undefined
at listing-element.ts:34
at Array.forEach (<anonymous>)
at SafeSubscriber._next (listing-element.ts:33)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
at SafeSubscriber.next (Subscriber.js:185)
at Subscriber._next (Subscriber.js:125)
at Subscriber.next (Subscriber.js:89)
at MapSubscriber._next (map.js:83)
at MapSubscriber.Subscriber.next (Subscriber.js:89)
at MapSubscriber._next (map.js:83)
这是我认为所有相关的信息,但如果有人需要更多信息,我非常乐意提供我能提供的任何信息。您可以按如下所示进行尝试
this.afAuth.authState.subscribe(
(auth) => {
if (auth != null) {
this.listingProvider.checkUnreadThreads(this.listing.id).subscribe(threads
=> {
threads.forEach(thread => {
if (thread[auth.uid].unread) { // <-Line 34 is here
this.unread = true;
}
})
})
})
}
});
this.afAuth.authState.subscribe(
(auth)=>{
如果(auth!=null){
this.listingProvider.checkUnreadThreads(this.listing.id).subscribe(threads
=> {
threads.forEach(线程=>{
如果(thread[auth.uid].unread){/在尝试访问unthread属性之前检查用户是否存在线程。如果您仍然面临此问题,请告诉我
if (thread[this.userId] && thread[this.userId].unread) {
this.unread = true;
}
返回{id,…data}
将把id和所有数据字段合并为一个
现在您的线程
如下所示
id:....,
unread:....,
fieldx:....,
fieldy:..... etc
试试这个代码
this.userId = this.auth.getUser().uid;
this.listingProvider.checkUnreadThreads(this.listing.id).subscribe(threads => {
threads.forEach(thread => {
if (thread.id == this.userId && thread.unread) {
this.unread = true;
}
})
})
谢谢,虽然示例本身不起作用,但它确实给了我一个想法,让我尝试将其包装到另一个if语句中,但只使用thread[this.userId]
作为条件,而不是thread[this.userId].unread
,并且它不再给我错误!很高兴它有帮助:)
this.userId = this.auth.getUser().uid;
this.listingProvider.checkUnreadThreads(this.listing.id).subscribe(threads => {
threads.forEach(thread => {
if (thread.id == this.userId && thread.unread) {
this.unread = true;
}
})
})