简体   繁体   English

从 Firestore 文档 ionic 读取值

[英]read values from firestore document ionic

I have a service which fetches a particular document from firestore using this method我有一项服务使用此方法从 Firestore 获取特定文档

getBidremains(userId: string){ return this.firestore.collection('userProfile').doc(userId); getBidremains(userId: string){ return this.firestore.collection('userProfile').doc(userId);

In the typscript class I am calling this method in ngOnInit as this.userInfo = this.firestoreService.getBidremains(userid).valueChanges().subscribe(data => console.log(data));在打字稿 class 中,我在 ngOnInit 中将此方法称为 this.userInfo = this.firestoreService.getBidremains(userid).valueChanges().subscribe(data => console.log(data));

In the console I can see the data is fetched correctly, but when i try to use this.userInfo.remainBids it prints nothing.在控制台中,我可以看到数据已正确获取,但是当我尝试使用 this.userInfo.remainBids 时,它什么也没打印。 In the console it shows as undefined.在控制台中它显示为未定义。

But in the html file( after removing.subscribe) i am able to print the correct value from firebase using {{ (userInfo | async)?.remainBids}} Can someone please help me out here, not sure what is it that I am doing wrong.但是在 html 文件中(removing.subscribe 之后)我可以使用 {{ (userInfo | async)?.remainBids}} 从 firebase 打印正确的值?做错了。 I want to fetch the document and be able to read values of the field contained in the document.我想获取文档并能够读取文档中包含的字段的值。

firestore database document fields are simple it contains email, name and other fields firestore 数据库文档字段很简单它包含 email、name 和其他字段

This is firestore service function name is getBidremains(userId: string)这是 Firestore 服务 function 名称是 getBidremains(userId: string)

This is the typescript class where the call is made this.userInfo = this.firestoreService.getBidremains(userid).valueChanges().subscribe(data => console.log(data));这是 typescript class 调用的地方 this.userInfo = this.firestoreService.getBidremains(userid).valueChanges().subscribe(data => console.log(data));

.subscribe() method returns a Subscription .subscribe()方法返回一个订阅

If you need this.userInfo.remainBids to have the remaining bids, use it inside the subscribe method()如果您需要 this.userInfo.remainBids 来获得剩余的出价,请subscribe 方法() 中使用它

this.userInfoSubscription = this.firestoreService.getBigremains(userid).valuechanges().subscribe(data => {
    this.userInfo = data;
});

Also, don't forget to unsubscribe the Subscription inside ngOnDestroy.另外,不要忘记在 ngOnDestroy 中取消订阅 Subscription。

The userinfo variable is assigned to a Subscription , which does not have a remainBids property. userinfo变量分配给Subscription ,该 Subscription 没有remainBids属性。 You either need to assign the variable inside the subscribe , like this:您需要在subscribe中分配变量,如下所示:

...subscribe(data => {this.userInfo = data})

or make your userInfo variable an Observable and assign it like this:或使您的userInfo变量成为 Observable 并像这样分配它:

this.userInfo = this.firestoreService.getBigremains(userid).valuechanges()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM