【问题标题】:Angular Firebase Getting ZoneAwarePromiseAngular Firebase 获取 ZoneAwarePromise
【发布时间】:2019-06-14 10:07:36
【问题描述】:

我正在尝试构建此功能以在我的应用程序的个人资料页面上上传个人资料图片,问题是由于图片上传功能,我得到了ZoneAwarePromise。我尝试了很多不同的方法,但无法改变结果。

有人可以帮我吗?我做错了什么?

uploadService.ts

pictureUpload(file: any, uid: string) {
    return this.afUpload.upload('profile/' + uid, file);
}

profile.component.ts

onCreateUser() {
    const picture = this.uploadService.pictureUpload(this.fileInput.nativeElement.files['0'], currentUserID).then(
      async (data) => {
        try {
          return data.ref.getDownloadURL();
        } catch {
          console.error();
        } finally {
          const email = this.userProfileForm.value.email;
          const firstName = this.userProfileForm.value.firstName;
          const lastName = this.userProfileForm.value.lastName;

          const userObj = {
            'uid': currentUserID,
            'email': email,
            'firstName': firstName,
            'lastName': lastName,
            'picture': picture
          };

          console.log('the object is');
          console.log(userObj);

          if (this.editMode) {
            return this.db.object('profile/' + currentUserID).update(userObj).then(
              () => {
                return this.presentToaster('Dados atualizados');
              },
              (e) => {
                this.presentToaster(e);
                console.error(e);
              }
            );
          } else {
            const load = await this.loadCtrl.create({
              message: 'Criando Usuario'
            });
            await load.present();
            return this.db.object('profile/' + currentUserID).set(userObj).then(
              () => {
                load.dismiss();
                return this.navCtrl.navigateRoot('news');
              },
              (error) => {
                load.dismiss();
                this.presentToaster(error);
                console.error(error);
              }
            );
          }
        }
      },
      (error) => {
        console.error(error);
      }
    );
  }

【问题讨论】:

    标签: angular firebase ionic-framework angularfire2


    【解决方案1】:

    我终于明白了。

    免责声明:这是我的第一个 Firebase 项目,因此以下所有描述均基于我的观察。如果我对任何事情有误,请纠正我,我希望能真正理解这一点。谢谢:)

    基本上,当我收到try 时,我希望能取回数据,在本例中为data.ref.getDownloadURL()。因为这将是一个承诺,所以我使用.then 方法接收“完整”数据,然后在该响应中运行其他所有内容。我似乎只存在那里的数据。

    onCreateUser(uForm: FormGroup) {
        const currentUserID = this.userAuth.getCurrentUserID();
        let picture;
        this.uploadService.pictureUpload(this.fileInput.nativeElement.files['0'], currentUserID).then(
          async (data) => {
            try {
              data.ref.getDownloadURL().then(
                async (downloadURL) => {
                  picture = downloadURL;
                  const email = this.userProfileForm.value.email;
                  const firstName = this.userProfileForm.value.firstName;
                  const lastName = this.userProfileForm.value.lastName;
    
                  const userObj = {
                    'uid': currentUserID,
                    'email': email,
                    'firstName': firstName,
                    'lastName': lastName,
                    'picture': picture
                  };
    
                  if (this.editMode) {
                    return this.db.object('profile/' + currentUserID).update(userObj).then(
                      () => {
                        return this.presentToaster('Dados atualizados');
                      },
                      (e) => {
                        this.presentToaster(e);
                        console.error(e);
                      }
                    );
                  } else {
                    const load = await this.loadCtrl.create({
                      message: 'Criando Usuario'
                    });
                    await load.present();
                    return this.db.object('profile/' + currentUserID).set(userObj).then(
                      () => {
                        load.dismiss();
                        return this.navCtrl.navigateRoot('news');
                      },
                      (error) => {
                        load.dismiss();
                        this.presentToaster(error);
                        console.error(error);
                      }
                    );
                  }
                  //
                }
              );
            } catch (error) {
              console.error(error);
            }
          },
          (error) => {
            console.error(error);
          }
        );
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多