Typescript 如何从firestore获取当前用户数据?
我正在进行一个Ionic 4项目,在该项目中,我希望在用户登录到应用程序后显示当前用户的所有帖子。目前,我正在使用Typescript 如何从firestore获取当前用户数据?,typescript,firebase,ionic-framework,google-cloud-firestore,angularfire2,Typescript,Firebase,Ionic Framework,Google Cloud Firestore,Angularfire2,我正在进行一个Ionic 4项目,在该项目中,我希望在用户登录到应用程序后显示当前用户的所有帖子。目前,我正在使用snapshotChanges()和subscribe()方法(如下所示)从firestore获取数据。但它提供了firestore文档中的所有数据。在两个文档上都有userID和postID引用,如何从该特定用户处获取author和desc的值,或者是否可能? 具体来说,如何显示包含作者和描述的用户的帖子(54d039ec-5b3c-4ee9-95a0-418b06365f25)
snapshotChanges()
和subscribe()
方法(如下所示)从firestore获取数据。但它提供了firestore文档中的所有数据。在两个文档上都有userID
和postID
引用,如何从该特定用户处获取author
和desc
的值,或者是否可能?
具体来说,如何显示包含作者和描述的用户的帖子(54d039ec-5b3c-4ee9-95a0-418b06365f25)
Firestore:
以下是我所做的:
getData.service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class getData {
constructor(
private firestore: AngularFirestore
) { }
readData() {
return this.firestore.collection('promotions').snapshotChanges();
}
}
post.ts
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-post',
templateUrl: './post.page.html',
styleUrls: ['./post.page.scss'],
})
export class PostPage implements OnInit {
constructor(
private afs: AngularFirestore
){}
ngOnInit() {
this.getData.readData().subscribe(data => {
this.posts = data.map(e => {
return {
id: e.payload.doc.id,
Author: e.payload.doc.data()['author'],
Description: e.payload.doc.data()['desc'],
};
})
console.log(this.posts);
});
}
}
post.html
<ion-card no-padding *ngFor="let post of posts">
<ion-card-header>
<ion-card-title>{{post.Author}}</ion-card-title>
</ion-card-header>
<ion-card-content>
<p>{{ post.Description }}</p>
</ion-card-content>
</ion-card>
更新
let user = this.afAuth.auth.currentUser;
uid = user.uid;
const docs = this.afs.collection('users');
const userinfo = docs.snapshotChanges()
.pipe(
map(actions =>
actions.map(a =>
{ const data =
a.payload.doc.data();
const id = a.payload.doc.id;
const Author = a.payload.doc.data()['author']
return { id, data, Author
};
})
)
);
console.log(userinfo);
输出:要获取
作者
和描述
,请尝试以下操作:
let user = this.afAuth.auth.currentUser;
uid = user.uid;
const docs = this.afs.collection('users');
const userInfo = docs.snapshotChanges().pipe(
map(data => {
return {
id: data.payload.doc.id,
Author: data.payload.doc.data()['author']
Description: data.payload.doc.data()['desc']
};
}));
let user = this.afAuth.auth.currentUser;
uid = user.uid;
this.afs.firestore.collection('posts').where('user', '==', uid).get().then(posts => {
postsCreatedByUser = posts.docs.map(e => {
return {
Author: e.data()['author'],
Description: e.data()['desc'],
};
}))
})
似乎您正在使用,首先将引用放在文档userid
,然后使用snapshotChanges
,它返回一个可观察的
,然后您可以检索数据。是Observable
类中的一个实例方法,用于将函数运算符缝合到一个链中(map
是这些运算符之一)。要获取用户创建的帖子的“作者”和“描述”,可以执行以下操作:
let user = this.afAuth.auth.currentUser;
uid = user.uid;
const docs = this.afs.collection('users');
const userInfo = docs.snapshotChanges().pipe(
map(data => {
return {
id: data.payload.doc.id,
Author: data.payload.doc.data()['author']
Description: data.payload.doc.data()['desc']
};
}));
let user = this.afAuth.auth.currentUser;
uid = user.uid;
this.afs.firestore.collection('posts').where('user', '==', uid).get().then(posts => {
postsCreatedByUser = posts.docs.map(e => {
return {
Author: e.data()['author'],
Description: e.data()['desc'],
};
}))
})
请注意,为了过滤帖子,我使用了.where()方法Hi@CODEr,根据屏幕截图,我不太确定您是否正确访问了数据。我认为您可能需要再次检查指向您试图访问的文档的引用。如果您从
posts
集合获取数据,最终目标是获取author
和desc
字段。您需要引用您的posted
文档,以便访问这些字段。另一方面,当访问users
集合并尝试访问posts
映射(其中包含0
字段)时,您需要确定哪个userID
文档。你需要知道documentID。@sllopis嗨,我想得到的是用户(6YbhQux2sgTH66F0cJpdcT87Pw83)
生成的posts
集合字段author
和desc
。嗨,我尝试了您的解决方案,但它显示“DocumentReference”类型上不存在错误属性“snapshotChanges”
在您的代码中什么是afs
,它应该是AngulareFirestore
Hi,它是AngularFirestore。我的代码从“@angular/fire/firestore”导入{AngularFirestore}
和私有afs:AngularFirestore
获取此错误属性“payload”在类型“DocumentChangeAction[]”上不存在trydocs.snapshotChanges().pipe(map(actions=>actions.map(a=>{const data=a.payload.doc.data());const id=a.payload.doc.id;返回{id,…data};})代码>