反应 FirebaseError DocumentReference.set()
React FirebaseError DocumentReference.set()
有人可以帮助我吗?我认为我处理承诺的方式是错误的,真的需要有人帮助我。
我正在让用户上传图片。当用户按下提交时,图像将上传到 firebase-storage。但是我不认为我正在处理上传图像并将数据设置为 firebase-database 的等待时间。我的意思是当我按下提交时我收到错误 FireBase Function DocumentReference.set() called with invalid data
因为它将图像设置为 undefined
但是,如果我等待几秒钟,我会收到 console.log("File available at" + downloadUrl)
,这意味着图片已上传。
基本上我只需要在上传图像和将数据发送到 firebase-database
之间的代码中添加一个等待期
这是我的代码任何帮助将不胜感激!!!!!
const uploadImage = async (uri, imageName) => {
const response = await fetch(uri)
const blob = await response.blob()
var ref = firebase.storage().ref().child(`images/${imageName}`)
ref.put(blob)
.then(()=>{
// Upload completed successfully, now we can get the download URL
var storageRef = firebase.storage().ref('images/' + imageName)
storageRef.getDownloadURL().then((downloadUrl)=>{
console.log(`File available at ${downloadUrl}`)
setDownload(JSON.stringify(downloadUrl))
})
})
.catch(error => {
setRefreshing(false) // false isRefreshing flag for disable pull to refresh
Alert.alert("An error occured", "Please try again later")
});
}
const handleSubmit = useCallback(() => {
if (postImage !== undefined) {
const fileExtention = postImage[0].split('.').pop()
const fileName = `${uniqid}.${fileExtention}`
uploadImage(postImage, fileName)
firebase.firestore()
.collection('Posts')
.doc(uniqid)
.set({
id: currentUser,
name: postName[0],
image: downloadImage,
})
}
})
预先感谢您的所有帮助!!!!!
要在 useCallback
中使用 await,您可以尝试将其中的代码包装在一个自调用函数中,如下所示:
const handleSubmit = useCallback(() => {
(async () =>{ if (postImage !== undefined) {
const fileExtention = postImage[0].split('.').pop()
const fileName = `${uniqid}.${fileExtention}`
uploadImage(postImage, fileName)
await firebase.firestore()
.collection('Posts')
.doc(uniqid)
.set({
id: currentUser,
name: postName[0],
image: downloadImage,
})
}
})()
})
有人可以帮助我吗?我认为我处理承诺的方式是错误的,真的需要有人帮助我。
我正在让用户上传图片。当用户按下提交时,图像将上传到 firebase-storage。但是我不认为我正在处理上传图像并将数据设置为 firebase-database 的等待时间。我的意思是当我按下提交时我收到错误 FireBase Function DocumentReference.set() called with invalid data
因为它将图像设置为 undefined
但是,如果我等待几秒钟,我会收到 console.log("File available at" + downloadUrl)
,这意味着图片已上传。
基本上我只需要在上传图像和将数据发送到 firebase-database
这是我的代码任何帮助将不胜感激!!!!!
const uploadImage = async (uri, imageName) => {
const response = await fetch(uri)
const blob = await response.blob()
var ref = firebase.storage().ref().child(`images/${imageName}`)
ref.put(blob)
.then(()=>{
// Upload completed successfully, now we can get the download URL
var storageRef = firebase.storage().ref('images/' + imageName)
storageRef.getDownloadURL().then((downloadUrl)=>{
console.log(`File available at ${downloadUrl}`)
setDownload(JSON.stringify(downloadUrl))
})
})
.catch(error => {
setRefreshing(false) // false isRefreshing flag for disable pull to refresh
Alert.alert("An error occured", "Please try again later")
});
}
const handleSubmit = useCallback(() => {
if (postImage !== undefined) {
const fileExtention = postImage[0].split('.').pop()
const fileName = `${uniqid}.${fileExtention}`
uploadImage(postImage, fileName)
firebase.firestore()
.collection('Posts')
.doc(uniqid)
.set({
id: currentUser,
name: postName[0],
image: downloadImage,
})
}
})
预先感谢您的所有帮助!!!!!
要在 useCallback
中使用 await,您可以尝试将其中的代码包装在一个自调用函数中,如下所示:
const handleSubmit = useCallback(() => {
(async () =>{ if (postImage !== undefined) {
const fileExtention = postImage[0].split('.').pop()
const fileName = `${uniqid}.${fileExtention}`
uploadImage(postImage, fileName)
await firebase.firestore()
.collection('Posts')
.doc(uniqid)
.set({
id: currentUser,
name: postName[0],
image: downloadImage,
})
}
})()
})