【发布时间】:2022-07-20 17:34:02
【问题描述】:
我正在该函数中编写一个函数 uploadPost() 我将一个文档添加到 firebase firestore 集合中,而不是选择一个图像并通过从 firebase 获取下载 url 来更新文档,但我收到类似的错误
FirebaseError: Function updateDoc() called with invalid data. Unsupported field value: a custom Promise object (found in field image in document posts/mcux46HsSK4mxycOIuev)
下面给出我的功能
const uploadPost = async () => {
if (loading) return;
setLoading(true);
const docRef = await addDoc(collection(db, 'posts'), {
username: session.user.username,
caption: captionRef.current.value,
profileImg: session.user.image,
timestamp: serverTimestamp(),
})
const imageRef = ref(storage, `posts/${docRef.id}/image`);
await uploadString(imageRef, selectedFile, 'data_url').then(async (snapshot) => {
const downloadUrl = getDownloadURL(imageRef);
await updateDoc(doc(db, 'posts', docRef.id), {
image: downloadUrl
});
});
setOpen(false);
setLoading(false);
setSelectedFile(null);
}
这有什么问题,请对此提出一些建议。
【问题讨论】:
-
getDownloadURL是异步函数吗?如果是这样,你必须await它来获取downloadUrl值,否则你只是在updateDoc调用中向image属性传递一个承诺(这可能会解释错误)。