好的,我的上有这样的结构:
Collection question:
- userID
- text
and Collection user:
- name
- key我可以轻松地从数据库中检索问题数据并返回它,但目前还没有用户数据。他们,我需要在数据库中做另一个搜索,前面返回的评论。然而,我在尝试这样做的时候遇到了很多问题。
第一:
我这样做是为了搜索问题:
构成部分:
export class ListQuestionsComponent implements OnInit {
tableData: Question[] = [];
userData: User[] = [];
constructor(
private questionService: QuestionService,
private userService: UserService,
) { }
ngOnInit() {
this.loadItens();
this.getUsers();
}
loadItens() {
this.questionService.loadItems().subscribe(response => {
this.tableData = [];
for (const item of response) {
this.tableData.push(item.payload.doc.data() as Question);
}
}问询服务:
loadItems() {
const query = ref => ref
.limit(10);
return this.firestore.collection('perguntas', query).snapshotChanges();
}这是可行的,现在我在我的tableData中有了问题。现在,我需要搜索每个问题的用户。
我尝试在相同的组件中这样做:
getUsers() {
for(const item of this.tableData) {
this.userService.getUserByKey(item.userID).subscribe(response => {
this.userData = [];
for (const user of response) {
this.userData.push(user.payload.doc.data() as User);
}
});
}
}用户服务
getUserByKey(key: string) {
return this.firestore.collection('users', ref => ref
.where('key', '==', key))
.snapshotChanges();
}最后,我有一个tableData,有10个问题,userData没有任何问题。我现在不知道该怎么办了。我只需要在我所寻求的问题中引用的用户。
发布于 2019-09-16 16:26:19
那么,对异步函数的研究已经取得了一定的成果。我在代码中改变了一些东西。
可变用户数据
userData: any;
在我的loadItems()的订阅中,我称之为getUsers()
和我的getUsers()
getUsers() {
for (const item of this.tableData) {
this.usuarioService.getUserByKey(item.userId).subscribe(response => {
this.userData[item.userId] = response[0].payload.doc.data();
});
}
}https://stackoverflow.com/questions/57948591
复制相似问题