【发布时间】:2021-12-15 16:34:14
【问题描述】:
我正在尝试将图像上传到 firebase 存储,然后将图像 url 添加到 firestore 数据库。
userId: string;
fullname: string;
phone: string;
userPhoto: string;
constructor(
private auth: AuthService,
public afauth: AngularFireAuth,
private afs: AngularFirestore,
private loadingCtrl: LoadingController,
private toastr: ToastController,
private angularFireStorage: AngularFireStorage
) { }
ngOnInit() {
this.auth.user$.subscribe(user => {
this.userId = user.userId;
this.fullname = user.userName;
this.phone = user.userPhone;
this.userPhoto = user.userPhoto;
})
}
imageName() {
const newTime = Math.floor(Date.now() / 1000);
return Math.floor(Math.random() * 20) + newTime;
}
async storeImage(imageData: any, location: string) {
try {
const imageName = this.imageName();
return new Promise((resolve, reject) => {
const pictureRef = this.angularFireStorage.ref(location + imageName);
pictureRef
.put(imageData)
.then(function () {
pictureRef.getDownloadURL().subscribe((url: any) => {
resolve(url);
console.log(url);
this.userPhoto = url //error
// this.afs.collection('user').doc(this.userId).set({
// 'userPhoto': url,
// 'editedAt': Date.now()
// }, {merge: true})
});
})
.catch((error) => {
reject(error);
});
});
} catch (e) {}
}
在我尝试为 userPhoto 变量赋值后,我收到了这个错误:TypeError: Cannot set properties of undefined (setting 'userPhoto')。我在发生这种情况的行的注释中写了错误。 有人可以帮助我我做错了什么?
【问题讨论】:
-
.then(function () {在这里使用箭头功能 -
@RobertoZvjerković 谢谢,它有效。
标签: angular typescript firebase