angular - Angular Firebase 获取 ZoneAwarePromise
问题描述
我正在尝试构建此功能以在我的应用程序的个人资料页面上上传个人资料图片,问题是我得到ZoneAwarePromise
了图片上传功能的结果。我尝试了很多不同的东西,但无法改变结果。
有谁能够帮我?我究竟做错了什么?
上传服务.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);
}
);
}
解决方案
我终于明白了。
免责声明:这是我的第一个 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);
}
);
}
推荐阅读
- python - Tkinter 主题和背景
- mysql - mysqli_real_connect(): (HY000/1045): Access denied for user 'phpmyadmin'@'localhost' (使用密码: YES)
- javascript - 在删除 baseHref Angular 后状态导航不起作用
- django - 如何为不具有相同值的 2 个字段添加约束?
- css - 文本与 justify-content: space-evenly 奇怪地对齐;
- prolog - Prolog - 如何将时间戳设置为谓词中的参数?
- sql - sqlite:如何以不平凡的方式修改列内容[示例]
- python - 如何在seaborn distplot的模式下画一条垂直线
- json - 获取地址的政治/学区
- java - AWS S3 SDK 源代码显示带有 UnsupportedOperationException 的 completeMultipartUpload()