I'm trying to upload an image to the firebase storage and after that add the image url to the firestore database.
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) {}
}
After I try to give an value to userPhoto variable, I got this error: TypeError: Cannot set properties of undefined (setting 'userPhoto'). I wrote error in comment in the line where this is happening. Somebody can help me what am I doing wrong?