FireBase Storage downloadURL未在Ionic 3中设置正确的值

时间:2018-04-06 09:15:25

标签: angular firebase ionic3 firebase-storage

我使用下面的代码块将音频文件发送到FireBase Storage然后我将这些记录添加到表中以保留每个帖子记录的下载链接。

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ı!");
    })
  }

但是downloadURL值没有设置正确的值,这里是此代码块的设置记录

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

1 个答案:

答案 0 :(得分:1)

成功上传后,

upload.downloadURL会发出类型字符串的Observable。首先订阅获取网址然后执行add

尝试

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);
    }
}