0

RealTime Databaseからのコメントを取得したいです。私はタスクを完了しましたが、オブジェクトはページへの復帰後にのみ表示されます(リフレッシュされません)。関数の非同期化で参照を使用してオブジェクトを取得

デフォルトangularfire2 constuctor blog.service.ts

constructor(public afDb: AngularFireDatabase) {} 

My機能DBの外のコメントを取得するための内部。これは多くの関数の一つに過ぎず、参照やオブジェクトをひとつの手続きで得る方法がわからないので、私はこのような関数を思いついた。もちろん

getComments(blogTitle: string) { 
    const blogsRef = this.afDb.database.ref('blog-pl'); 
    let commentsRef = null; 
    blogsRef.orderByChild('title').equalTo(blogTitle).on("child_added", function (snapshot) { 
     if (snapshot.hasChild('comments')) { 
     commentsRef = snapshot.child('comments').ref; 
     } 
    }); 
    return this.afDb.list<Comment>(commentsRef, ref => 
     ref.orderByChild('timestamp') 
    ).valueChanges(); 
    } 

私は[]観測のコメントを取得したいが、その代わり、私はnullを取得します。問題は、commentsRefが値を返す前に

this.afDb.list<Comment>(commentsRef,... 

になることです。私はcommentsRefへの参照を割り当てる関数の中でそれを返そうとしました。しかし、それは を呼び出します。エラー:(56、9)TS2347:型指定されていない関数呼び出しは型引数を受け付けません。 私は解決できません。

comments.component.tsあなたのアドバイスやあなたの答えに感謝を待っている

<ul *ngFor="let curUserReview of (currentUsersReviews | async)?.slice().reverse()"> 

を使用してHTMLでそれを表示する

public currentComments: Observable<Comment[]>; 

constructor(private blogService: BlogService, private authServicec: AuthService) {} 

ngOnInit(): void { 
    this.currentComments = this.blogService.getComments(this.blogTitle); 
    this.currentComments.subscribe(val => { 
     if (val.length != 0) { 
     this.areAnyComms = true; 
     }}) 
    } 

でそれを取得しようとしています。

答えて

0

データが非同期になるため、最初のgetの内部に配置する必要があります。 try

getComments(blogTitle: string) { 
const blogsRef = this.afDb.database.ref('blog-pl'); 
let commentsRef = null; 
return blogsRef.orderByChild('title').equalTo(blogTitle).on("child_added", function (snapshot) { 
    if (snapshot.hasChild('comments')) { 
    commentsRef = snapshot.child('comments').ref; 
    return this.afDb.list<Comment>(commentsRef, ref => 
     ref.orderByChild('timestamp') 
    ).valueChanges(); 
    } 
}); 
} 
+0

私が言ったように、TS2347:型指定されていない関数呼び出しは型引数エラーを受け付けないことがあります。 – ImmoXZ

+0

あなたの関数 'getComments(blogTitle:string):Observable {type}に型を追加してみてください。 – Hareesh

+0

残念ながらまだ動作していません(オブジェクトのタイプが間違っています)。私はそれらのオブジェクトを得ることができる他のオプションはありますか? – ImmoXZ

関連する問題