[英]How to get current user data from firestore?
我正在开发一个 Ionic 4 项目,我想在用户登录应用程序后显示当前用户的所有帖子。 目前,我正在使用snapshotChanges()
和subscribe()
方法(如下所示)从 firestore 获取数据。 但它为我提供了 firestore 文档中的每一个数据。 在两个文档上都有userID
和postID
引用,我怎样才能从该特定用户那里获得author
和desc
的值,或者有可能吗? 具体来说,如何显示包含author
和desc
的用户(6YbhQux2sgTH66F0cJpdcT87Pw83)
的帖子(54d039ec-5b3c-4ee9-95a0-418b06365f25)
?
Firestore: firestore 映像
这是我所做的:
获取数据服务.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);
});
}
}
后.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>
我还尝试使用afAuth.auth.currentUser
获取当前用户 ID 和帖子,但它显示错误Property 'map' does not exist on type 'DocumentSnapshot'
。 我是新手,不知道如何进行。
let user = this.afAuth.auth.currentUser;
uid = user.uid;
this.afs.firestore.collection('users').doc(uid).get()
.then(doc=>{
var a = doc.data().posts
console.log(a);
this.afs.firestore.collection('posts').doc()
.get().then(doc2=>{
this.promotions = doc2.map(e=>{
return {
id: e.payload.doc2.id,
Author: e.payload.doc2.data()['author'],
Description: e.payload.doc2.data()['desc'],
};
})
})
})
更新
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);
Output :用户信息
为了获取用户创建的帖子的“作者”和“描述”,您可以执行以下操作:
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() 方法
要获取author
和desc
,请尝试以下操作:
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']
};
}));
看来您正在使用angularfire
,首先将引用放在文档userid
中,然后使用返回Observable
的snapshotChanges
,然后您可以检索数据。 Pipe
是Observable
class 中的一个实例方法,用于将功能运算符拼接成一个链( map
是其中一个运算符)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.