【发布时间】:2022-01-21 00:19:09
【问题描述】:
我正在努力强制代码同步。该代码旨在使用 vue 可组合上传图像,等待上传成功,然后将 url 从 firebase 存储存储到数据库中。我能做的最好的就是让代码正常工作,但成功代码在上传完成之前触发(虽然我得到了 url)。
下面的代码不起作用,但我尝试使用 then 回调将动作链接在一起,以强制它们以同步方式运行。不工作。
VueComponent.vue
const newImage = async () => {
if (image.value) {
await uploadImage(image.value);
} else return null;
};
const handleSubmit = async () => {
try {
const colRef = collection(db, "collection");
newImage()
.then(() => {
addDoc(colRef, {
content: content.value
});
})
.then(() => {
//code to run only on success
});
});
} catch (error) {
}
};
useStorage.js 可组合
import { ref } from "vue";
import { projectStorage } from "../firebase/config";
import {
uploadBytesResumable,
getDownloadURL,
ref as storageRef,
} from "@firebase/storage";
const useStorage = () => {
const error = ref(null);
const url = ref(null);
const filePath = ref(null);
const uploadImage = async (file) => {
filePath.value = `${file.name}`;
const storageReference = storageRef(projectStorage,
filePath.value);
//<--I want this to be synchronous, but it isn't.
const uploadTask = uploadBytesResumable(storageReference,
file);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) *
100;
console.log("Upload is " + progress + "% done");
},
(err) => {
},
() => {
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL)
=>
{console.log("File available at", downloadURL);
});
}
);
};
return { url, filePath, error, uploadImage };
};
export default useStorage;
【问题讨论】:
-
“我正在努力强制代码异步。”您现在已经多次说过这句话,并且:1) 您可能的意思是强制它为 同步,2) 使异步代码同步是不可能的。它始终是异步的。
-
“不工作”和“不工作”真的很难帮助。代码在运行时做什么?你期望它做什么?如果你在每一行都设置了一个断点,然后在调试器中运行,这是 first 行,它不会像你预期的那样工作。
-
谢谢你,@FrankvanPuffelen。你说得对,我的意思是“同步”。当handleSubmit 触发时,newImage() 函数触发但不等待uploadTask 完成,所以它直接进入addDoc()。我希望在 addDoc 开始之前完成 uploadTask。
标签: javascript vue.js google-cloud-firestore firebase-storage