2017-01-03 22 views
1

私は、firefbase2からfirefbase2を使ってデータを取得し、地図を作成し、データを更新/チェックしてからもう一度保存しますが、この奇妙な問題が発生しています'this.fs.getRiders'は未定義ですか?しかし、私はストリームを作成するためにサービスを使用しています、私は実際に何が起こっているのか分かりません。AngularFire 2ストリームからAngular 2サービスコールが定義されていませんか?

HERESにいくつかのコード

@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) 

誰でもアイデアがありますか?

+0

@RaiVu単純なマークダウンの引用符でエラーを再フォーマットすると、スタックトレースを読みにくくなります。このような編集をしないでください。 – cartant

答えて

5

thisは、この変更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 
    }) 
} 

roundUpdatessaveRound

以内に、現在のクラスのインスタンスで別の方法を指し続ける期待するところを指していない矢印の機能を使用しているが、あなたの具体的なケースではあまり便利ではない

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 
    }) 
} 
+1

この場合、パラメータの数は重要ではないので、 'bind'がより便利です。他のすべてのケースでは、矢印機能を使用するIMHOが優先されています(すでに私の答えに追加されています)。あなたのフィードバックをありがとう:) –

+0

ようこそ。あなたがそれを働かせることを聞いてうれしいです。 –

関連する問題