我想从此函数获取downloadURL。但是,它迫不及待想要得到它。因此,此函数不会返回downloadURL。 我怎么得到它?
uploadProfileImage = async (uid, file) => {
let userRef = this.str.ref(`${uid}`).child(`images/avatar.png`);
const uploadTask = userRef.put(file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, snapshot => {
let progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED:
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING:
console.log('Upload is running');
break;
default:
}
}, error => {
console.log('[Error] ', error);
}, async () => {
const downloadUrl = await uploadTask.snapshot.ref.getDownloadURL()
// update firebase database
this.updateUserInfo(uid, { profileImage: downloadUrl });
});
}
答案 0 :(得分:0)
一种方法是简单地跟踪由Google here提供的文档
uploadProfileImage = (uid, file) => {
let userRef = this.str.ref(`${uid}`).child(`images/avatar.png`);
const uploadTask = userRef.put(file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, snapshot => {
let progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED:
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING:
console.log('Upload is running');
break;
default:
}
}, error => {
console.log('[Error] ', error);
}, () => {
uploadTask.snapshot.ref.getDownloadURL().then((downloadUrl)=>{
// update firebase database
this.updateUserInfo(uid, { profileImage: downloadUrl });
}
});
}
我认为您的变量正确