Saurabh Kumar
Saurabh Kumar

Reputation: 16661

Firebase storage API triggers twice

I am trying to store my file using Google Firebase storage API but the pipe is getting executed twice. Any ideas to make it triggered one time only?

// def
task: AngularFireUploadTask;
storage: AngularFireStorage;

// code
this.task = this.storage.upload(path, file, { customMetadata });
    return this.task.snapshotChanges().pipe(
        map(doc => {
            console.log('me'); // THIS IS PRINTED TWICE
            return doc;
        })
    );

Upvotes: 1

Views: 446

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317562

According to the documentation, you can expect that the pipe will receive multiple snapshots as the upload progresses. Each of these snapshots contain some information about the status of the upload as it progresses over time.

If you just want to know when the upload is complete, take the code from the sample in the documentation, which gets the download URL after the upload is complete:

task.snapshotChanges().pipe(
    finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80924

You can use the operator take():

Emits only the first count values emitted by the source Observable.

Returns MonoTypeOperatorFunction<T>: An Observable that emits only the first count values emitted by the source Observable, or all of the values from the source if the source emits fewer than count values.

Example:

return this.task.snapshotChanges().pipe(
  take(1),
  map(doc => {
    console.log('me');
    return doc;
  })
)

https://www.learnrxjs.io/operators/filtering/take.html

Upvotes: 0

Related Questions