我有一个react native应用程序,我正在尝试使用firestore保存一个帖子。我使用的是@react-native-firebase/firestore,我使用以下代码:'
import firebase from "@react-native-firebase/app";
import "@react-native-firebase/firestore";
import "@react-native-firebase/storage";
export const saveMediaToStorage = (media, path) =>
new Promise((resolve, reject) => {
const fileRef = firebase.storage().ref().child(path);
fetch(media)
.then((response) => response.blob())
.then((blob) => fileRef.put(blob))
.then((task) => {
task.ref.getDownloadURL();
})
.then((downloadUrl) => {
// console.log(downloadUrl);
resolve(downloadUrl);
})
.catch((error) => {
console.log(error);
reject();
});
});
Response, blob和task看起来都很好,task变量看起来像这样:
Object {
"bytesTransferred": 2274761,
"metadata": Object {
"bucket": "_______.appspot.com",
"contentDisposition": "inline; filename*=utf-8''image2",
"contentEncoding": "identity",
"contentType": "image/jpeg",
"fullPath": "post/*****/****/image1",
"generation": "1672255330334821",
"md5Hash": "vVYL9wVo/***==",
"metageneration": "1",
"name": "image2",
"size": 2274761,
"timeCreated": "2022-12-28T19:22:10Z",
"updated": "2022-12-28T19:22:10Z",
},
"state": "success",
"totalBytes": 2274761,
}
我也检查了firebase,看到图像保存在存储中,但看起来问题在于下载url。
task.ref. getdownloadurl()返回undefined,因为task。Ref未定义。知道哪里出了问题吗?
我一直在一个博览会管理的工作流程,并使用firebase/firebase-storage,它工作得很好,但我最近弹出并切换到一个裸工作流在android,并开始使用@react-native firebase/firestore代替,这是当它崩溃。我为android (READ/WRITE_EXTERNAL_STORAGE)添加了所需的权限,但我想知道我是否错过了一些android特定的东西,或者@react-native-firebase的语法是否不同。
我在做以下操作时遇到了同样的问题:
let uploadTask = ref.put(blob, {customMetadata: newMetadata});
return uploadTask
.then((taskSnapshot) => {
if (taskSnapshot.state === firebase.storage.TaskState.SUCCESS) {
return taskSnapshot.ref.getDownloadURL(); // Error
} else {
throw new Error("Upload error")
}
})
.finally(() => {
blob.close();
});
会得到ref is undefined。我通过使用之前定义的ref
让它工作let uploadTask = ref.put(blob, {customMetadata: newMetadata});
return uploadTask
.then((taskSnapshot) => {
if (taskSnapshot.state === firebase.storage.TaskState.SUCCESS) {
return ref.getDownloadURL(); // Success
} else {
throw new Error("Upload error")
}
})
.finally(() => {
blob.close();
});