为什么我的firebase/angularfire查询返回可观察到的而不是数据?
我的其他 Firebase 查询返回数据,但我今天编写的查询返回 Observable
而不是数据。这意味着什么?为什么不返回数据? Observable 是否就像一个未兑现的承诺,意味着我们正在等待数据库发送数据?
import { Component, Input, Inject, } from '@angular/core';
import { Observable } from 'rxjs';
// Firebase
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/compat/firestore';
// types
import { L2Language } from '../../services/home-toolbar/home-toolbar.service';
import { LearningModes } from '../../services/home-toolbar/home-toolbar.service';
// services
import { HeaderToolbarService } from '../../services/header-toolbar/header-toolbar.service';
export interface ClipInMovieWatching { clip: number; };
@Component({
selector: 'app-videos-vocabulary',
templateUrl: './videos-vocabulary.component.html',
styleUrls: ['./videos-vocabulary.component.css']
})
export class VideosVocabularyComponent {
userUid: string | undefined = undefined;
private clipInMovieWatchingDoc: AngularFirestoreDocument<ClipInMovieWatching>;
clipInMovieWatching: Observable<ClipInMovieWatching | undefined>;
constructor(
private firestore: AngularFirestore,
@Inject(HeaderToolbarService) private headerToolbarService: HeaderToolbarService
) { }
videoSelector() {
this.headerToolbarService.getUser().subscribe((user: any) => {
this.userUid = user.uid;
console.log(this.userUid); // √ this works
this.clipInMovieWatchingDoc = firestore.collection('Users').doc(this.userUid).collection('English').doc('Videos').collection('Walker_Climbs_a_Tree').doc<ClipInMovieWatching>('Clip_In_Movie_Watching');
this.clipInMovieWatching = this.clipInMovieWatchingDoc.valueChanges();
console.log(this.clipInMovieWatching);
console.log(this.clipInMovieWatching.clip);
})
}
}
这是我的 Firestore 数据库:
我期望
console.log(this.clipInMovieWatching);
返回一个对象,但返回一个 Observable。
我期望
console.log(this.clipInMovieWatching.clip);
返回 5
但收到错误
Property 'clip' does not exist on type 'Observable<ClipInMovieWatching | undefined>'.
Do I need to put a ?
某处(因为类型 undefined
上不存在属性)?
我为文档 ClipInMovieWatching
制作了一个界面
,它只有一个属性clip
。
我使用 Firebase Auth 方法从组件中引入了 userUid
。
我创建了两个变量 clipInMovieWatchingDoc
和 clipInMovieWatching
。
当调用 videoSelector()
方法时,它会获取 userUid
,然后调用 Firestore。 Firestore 路径是深度嵌套的,但我已经检查了一百万次,并且尝试使用另一个文档的较短路径并得到相同的结果。
My other Firebase queries are returning data but a query I wrote today returns an Observable
instead of data. What does that mean? Why doesn't it return data? Is an Observable like an unfulfilled promise, meaning that we're waiting for the database to send the data?
import { Component, Input, Inject, } from '@angular/core';
import { Observable } from 'rxjs';
// Firebase
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/compat/firestore';
// types
import { L2Language } from '../../services/home-toolbar/home-toolbar.service';
import { LearningModes } from '../../services/home-toolbar/home-toolbar.service';
// services
import { HeaderToolbarService } from '../../services/header-toolbar/header-toolbar.service';
export interface ClipInMovieWatching { clip: number; };
@Component({
selector: 'app-videos-vocabulary',
templateUrl: './videos-vocabulary.component.html',
styleUrls: ['./videos-vocabulary.component.css']
})
export class VideosVocabularyComponent {
userUid: string | undefined = undefined;
private clipInMovieWatchingDoc: AngularFirestoreDocument<ClipInMovieWatching>;
clipInMovieWatching: Observable<ClipInMovieWatching | undefined>;
constructor(
private firestore: AngularFirestore,
@Inject(HeaderToolbarService) private headerToolbarService: HeaderToolbarService
) { }
videoSelector() {
this.headerToolbarService.getUser().subscribe((user: any) => {
this.userUid = user.uid;
console.log(this.userUid); // √ this works
this.clipInMovieWatchingDoc = firestore.collection('Users').doc(this.userUid).collection('English').doc('Videos').collection('Walker_Climbs_a_Tree').doc<ClipInMovieWatching>('Clip_In_Movie_Watching');
this.clipInMovieWatching = this.clipInMovieWatchingDoc.valueChanges();
console.log(this.clipInMovieWatching);
console.log(this.clipInMovieWatching.clip);
})
}
}
Here's my Firestore database:
I'm expecting
console.log(this.clipInMovieWatching);
to return an object but returns an Observable.
I'm expecting
console.log(this.clipInMovieWatching.clip);
to return 5
but I get an error
Property 'clip' does not exist on type 'Observable<ClipInMovieWatching | undefined>'.
Do I need to put a ?
somewhere (because no property exists on the type undefined
)?
I made an interface
for the document ClipInMovieWatching
which has only a single property clip
.
I brought the userUid
in from the component with the Firebase Auth methods.
I make two variables clipInMovieWatchingDoc
and clipInMovieWatching
.
When the method videoSelector()
is called it gets the userUid
and then calls Firestore. The Firestore path is deeply nested but I've checked this a million times, and I tried using a shorter path to another document and got the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它起作用。
在视图中,我将其提出:
在视图中显示值。这表明Firestore查询正在起作用。
要使用组件控制器中的值,我在a 关于
valuechanges
。这是一个角度事件,而不是壁炉功能。我改变了这些行
:
I got it working.
In the view I put this:
The value displays in the view. This shows that the Firestore query is working.
To use the value in the component controller I found the answer in a blog post about
ValueChanges
. This is an Angular event, not a Firebase feature.I changed these lines:
to