[英]Angular 2 Service call is undefined from Angularfire 2 stream?
我有一個操作,其中我使用angularfire2從firebase中獲取了一些數據,將其映射並對數據進行了一些更新/檢查,然后我想再次保存它,但是我遇到了一個奇怪的問題,它告訴我“ this.fs” .getRiders'是未定義的? 但是我正在使用服務來創建流,因此我不確定在此發生什么。
這是一些代碼
@Injectable()
export class RoundService {
public currentRound:FirebaseListObservable<any>;
constructor(private af: AngularFire, private as:AuthService, private fs:FirebaseService) { }
pullCurrentRound(serieUid:string){
return this.af.database.object(`series/${serieUid}/currentRound`)
.flatMap((res)=>{
return this.af.database.object(`rounds/${res.$value}`)
.map((res)=>res)
.do(this.roundUpdates)
.do(this.saveRound)
})
}
saveRound(round){
this.fs.getRiders.update(round.uid,round)
.then(snap=>{
console.log(snap)
})
}
和錯誤
Uncaught TypeError: Cannot read property 'getRiders' of undefined
at SafeSubscriber.RoundService.saveRound [as _next] (round.service.ts:57)
at SafeSubscriber.__tryOrSetError (Subscriber.js:232)
at SafeSubscriber.next (Subscriber.js:174)
at Subscriber._next (Subscriber.js:125)
at Subscriber.next (Subscriber.js:89)
at DoSubscriber._next (do.js:82)
at DoSubscriber.Subscriber.next (Subscriber.js:89)
at DoSubscriber._next (do.js:87)
at DoSubscriber.Subscriber.next (Subscriber.js:89)
at MapSubscriber._next (map.js:83)
有人有想法嗎?
this
並不指向您期望的位置
pullCurrentRound(serieUid:string){
return this.af.database.object(`series/${serieUid}/currentRound`)
.flatMap((res)=>{
return this.af.database.object(`rounds/${res.$value}`)
.map((res)=>res)
.do(this.roundUpdates.bind(this)) // <<< changed
.do(this.saveRound.bind(this) // <<< changed
})
}
隨着這一變化this
保持在中當前類的實例指向roundUpdates
和saveRound
一種替代方法是使用箭頭功能,但在您的具體情況下它們不太方便
pullCurrentRound(serieUid:string){
return this.af.database.object(`series/${serieUid}/currentRound`)
.flatMap((res)=>{
return this.af.database.object(`rounds/${res.$value}`)
.map((res)=>res)
.do(x => roundUpdates(x)) // <<< changed
.do(round => this.saveRound(round) // <<< changed
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.