我正在使用Firestore构建一个项目,并且只停留其中一部分。我创建了一项上传文件的服务,效果很好,并且我将pipe
与last()
和concatMap
结合使用,以从Firestore返回getDownloadURL()
。
我遇到的问题是因为这是一个可观察到的问题,我无法获取该值并将其分配给我创建的要推送到数据库的对象。有人可以帮我吗?
以下是文件:
export class AddComponent {
uploadPercentage: Observable<number>;
logoURL: Observable<string>;
onLogoChanged(event) {
this.venueID = this.venueService.createID();
const logo: File = event.target.files[0];
const path = 'venues/logo/' + this.venueID + '/' + logo.name;
const task = this.uploadService.uploadFile(logo, path);
this.uploadPercentage = task.percentageChanges();
this.logoURL = task.snapshotChanges().pipe(
last(),
concatMap(() => task.task.snapshot.ref.storage.ref(path).getDownloadURL())
);
}
onFormSubmit() {
const venueObject = { creationDate: Date.now(), isVerified: false, logo: this.logoURL, ...this.venueForm.value };
console.log(venueObject);
this.venueService.createNewVenue(venueObject)
.then(
() => {
this.dismiss();
this.venueForm.reset();
},
(e) => console.error(e)
);
}
}
upload.service.ts
export class UploadService {
uploadPercent: Observable<number>;
constructor(private uploadDB: AngularFireStorage) {}
uploadFile(file: File, path: string) {
return this.uploadDB.upload(path, file);
}
}