我认为这是因为我实际上不知道如何正确地做自己想做的事情。基本上,我要保存用户与学校(也是用户)之间的关联,并且在新文档中要从学校用户中提取一些图像网址。我的查询都返回undefined ...
然后我基本上想将这些图像网址保存在我将要使用的另一个文档中。
结果可以很好地打印到控制台上。...
private addUserSchool(userId: string, school: any) {
const concatId = this.concatIds(userId, school.uid);
const userSchoolsRef: AngularFirestoreDocument<any> = this.afs.doc(`userSchools/${concatId}`);
const schoolRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${school.uid}`);
let schoolImageURL;
let schoolImageLogoURL;
if(school.active) {
schoolRef.valueChanges().subscribe(result => {
console.log(result)
return schoolImageURL = result.schoolImageURL;
});
}
schoolRef.valueChanges().subscribe(result => {
console.log(result)
return schoolImageLogoURL = result.schoolImageLogoURL;
})
// create model from this.
const data = {
userId: userId,
schoolId: school.uid,
active: school.active ? school.active : false,
activeSchoolImageURL: schoolImageURL ? schoolImageURL : null,
activeSchoolLogoURL: schoolImageLogoURL ? schoolImageLogoURL : null
}
return userSchoolsRef.set(data, { merge: true })
}
答案 0 :(得分:0)
如注释中所述,当您进行set
调用时,这些值尚不可用。
当第一个调用的值可用时,您可以使用另一个rxjs运算符执行set
:
private addUserSchool(userId: string, school: any): any {
const concatId = this.concatIds(userId, school.uid);
const userSchoolsRef: AngularFirestoreDocument<any> = this.afs.doc(`userSchools/${concatId}`);
const schoolRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${school.uid}`);
schoolRef.valueChanges()
.pipe(
take(1),
switchMap(schoolData => {
const data = {
userId: userId,
schoolId: school.uid,
active: school.active,
activeSchoolImageURL: school.active ? schoolData.schoolImageURL : null,
activeSchoolLogoURL: schoolData.schoolImageLogoURL
};
return of(userSchoolsRef.set(data, { merge: true }));
})
)
.subscribe();
}