0

I'm having issue in implementing the logic of saving image to firebase, getting back the url to the form img value and save the form value in the database, the issue is that while I get the form url updated correctly inside the firebase saving subscription, I get the same form url empty in the response of the second saving sub!

code:

  upload(): void {
    const file = this.selectedFiles.item(0);
    this.selectedFiles = undefined;
    let currentFileUpload = new FileUpload(file);

    this.pushedFile$ = this.service.pushFileToStorage(currentFileUpload).pipe(
      map(url => {
        this.service.form.patchValue({ img: url });
        console.log("inside upload:", this.service.form.value)
      })
    );
  }

  onSubmit() {

    this.upload();
    
    let save$ = this.service.save(this.service.form.value).pipe(map(data => console.log("save data: ", data) ));

    concat(this.pushedFile$, save$).subscribe((data => console.log("subscription: ", data)));
  }

  pushFileToStorage(fileUpload: FileUpload): Observable<any> {
    const filePath = `${this.basePath}/${fileUpload.file.name}`;
    const storageRef = this.firebaseStorage.ref(filePath);
    const uploadTask = this.firebaseStorage.upload(filePath, fileUpload.file);

    return <Observable<any>>uploadTask.snapshotChanges().pipe(
      last(),
      mergeMap(() => {
        return storageRef.getDownloadURL().pipe(
          map((url) => url)
        )
      })
    );
}

inspector output:

licons/v109/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2 abstract-form.component.ts:43 inside upload: {id: null, name: 'Sudan', nameAr: null, quantityPerUnit: null, img: 'https://firebasestorage.googleapis.com/v0/b/pos-ad…=media&token=960e9251-6ef8-4e01-8efa-a54745f9906f', …}averageCost: nullbarcode: nullcategory: nulldiscount: nullid: nullimg: "https://firebasestorage.googleapis.com/v0/b/pos-admin-f8a9f.appspot.com/o/products%2Fmaryam.jpg?alt=media&token=960e9251-6ef8-4e01-8efa-a54745f9906f"name: "Sudan"nameAr: nullquantity: nullquantityPerUnit: nullsellPrice: 11supplier: nullvatPercentage: nullvatValue: null[[Prototype]]: Object abstract-form.component.ts:54 subscription: undefined abstract-form.component.ts:52 save data: {id: 132, name: 'Sudan', nameAr: null, img: null, barcode: null, …}averageCost: nullbarcode: nullcategory: nulldepreciation: nulldiscount: nullid: 132img: nullname: "Sudan"nameAr: nullquantity: nullquantityPerUnit: nullsellPrice: 11supplier: nullvatPercentage: nullvatValue: null[[Prototype]]: Object abstract-form.component.ts:54 subscription: undefined

1 Answer 1

0

I managed to get it working using concatAll()

let pushedFile$ = this.service.pushFileToStorage(currentFileUpload).pipe(map(url => item["img"] = url));

let combined$ = of(pushedFile$, save$);

combined$.pipe(concatAll()).subscribe();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.