为什么我的firebase/angularfire查询返回可观察到的而不是数据?

发布于 2025-01-18 04:38:25 字数 2886 浏览 0 评论 0原文

我的其他 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

我创建了两个变量 clipInMovieWatchingDocclipInMovieWatching

当调用 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2025-01-25 04:38:25

我让它起作用。

在视图中,我将其提出:

<div>
    Clip: {{ (clipInMovieWatching | async)?.clip }}
</div>

在视图中显示值。这表明Firestore查询正在起作用。

要使用组件控制器中的值,我在a 关于valuechanges。这是一个角度事件,而不是壁炉功能。

我改变了这些行

console.log(this.clipInMovieWatching);
console.log(this.clipInMovieWatching.clip);

 this.clipInMovieWatching.subscribe(x => {
        console.log(x?.clip);
      })

I got it working.

In the view I put this:

<div>
    Clip: {{ (clipInMovieWatching | async)?.clip }}
</div>

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:

console.log(this.clipInMovieWatching);
console.log(this.clipInMovieWatching.clip);

to

 this.clipInMovieWatching.subscribe(x => {
        console.log(x?.clip);
      })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文