Umut Gür
Umut Gür

Reputation: 387

FireBase Storage downloadURL is not setting the right value in Ionic 3

I use the code block below to send an audio file to FireBase Storage then I add these records in a table to keep the download links of each post record.

private doPost() {
    const fileName = this.navParams.get('name').replace(/^.*[\\\/]/, '');
    const filePath = 'file://' + this.navParams.get('path');
    const fullPath = this.navParams.get('name');

    this.file.resolveDirectoryUrl(filePath).then((rootDir) => {
      this.file.getFile(rootDir, fileName, { create: false }).then((fileEntry) => {
        fileEntry.file((file) => {
          const reader = new FileReader();
          reader.onloadend = (res: any) => {
            let result = res.target.result
            let blob = new Blob([new Uint8Array(result)], { type: 'audio/mp3' })
            const upload = this.storage.ref('files/' + fileName).put(blob);
            console.log("download url " + upload.downloadURL);
            const postModel = {
              uID: this.uID,
              text: this.postText,
              time: new Date().getTime().toString(),
              downloadURL: upload.downloadURL.toString()
            }
            this.dataProvider.add(postModel, '/Post/');
            // this.dataProvider.addPost(postModel);
          }
          reader.readAsArrayBuffer(file);
        })
      })
      .catch((err) => {
        console.log("Get File'da hata: " + err)
      })
    })
    .catch((err) => {
      console.log("Directory Hatalı!");
    })
  }

But the downloadURL value is not setting right value, here is the setted record by this code block

downloadURL: "function () { return Object(__WEBPACK_IMPORTED_MODULE_2_rxjs_observable_from__[\"from\"])(task.then(function (s) { return s.downloadURL; })); }"

Upvotes: 0

Views: 347

Answers (1)

Hareesh
Hareesh

Reputation: 6900

upload.downloadURL emits Observable of type string after successful upload. First subscribe to get the url then do the add.

Try

upload.downloadURL().subscribe(url=>{
    if(url){
        const postModel = {
          uID: this.uID,
          text: this.postText,
          time: new Date().getTime().toString(),
          downloadURL: url
        }
        this.dataProvider.add(postModel, '/Post/');
        // this.dataProvider.addPost(postModel);
    }
}

Upvotes: 1

Related Questions