【发布时间】:2021-06-20 14:27:11
【问题描述】:
您好,我使用 firestorage 保存图像,然后使用 if 发送带有 url 的信息,然后在条件之后获取 url,我知道 then 是一个承诺和异步,但我需要我的条件等待然后我不知道我的条件如何才能在函数内部等待然后完成?
event.preventDefault();
let file = enterFileImage.imageFile;
imageUploader(file);
if (enterPhone.imageUrl != null) {
const db = firebase.firestore();
const docRef = db
.collection("phones")
.add({
...enterPhone,
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
});
}
图片上传器
const storageRef = firebase.storage().ref("/images");
const imageRef = storageRef.child(enterFileImage.imageName);
await imageRef.put(file).then((snapshot) => {
snapshot.ref
.getDownloadURL()
.then((url) => {
console.log(url)
setPhone((Prevstate) => ({
...Prevstate,
imageName: enterFileImage.imageName,
imageUrl: url,
}));
})
.catch((error) => {
console.log(error);
return false;
});
});
【问题讨论】:
标签: javascript reactjs firebase firebase-storage