将映像上载到存储后的firebase更新数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将映像上载到存储后的firebase更新数据库相关的知识,希望对你有一定的参考价值。

我正在使用React-native,Redux。

npm“firebase”:“^ 4.6.0”“react-native-fetch-blob”用于blobing图像

我正在尝试上传图像,然后使用firebase将图像URL添加到数据库。

我可以将图像上传到存储或更新数据库中的数据,但我不能同时执行这些操作。

上传图片后,我调用更新数据并且没有响应(成功/失败)

这两个功能单独工作100%,我不能一个接一个地称它们。我需要清楚吗?重启?如果是的话,怎么样?

上传图片:

const db = firebase.firestore();...
db.collection("reports").doc(uId).collection("user-reports")
        .add({
            "title": "report title...",
            "discription": "discription..."})
        .then(() => {
            console.log("Document successfully written!");
            dispach({
                type: types.FIREBASE_REPORT_ADD,
                payload: "Added Report"
            })
        })
        .catch((err) => {
            console.error("Error writing document: ", err);
        });

上传数据:

const imageRef = storageRef.child(`images/${uId}/${CREATION_TIME}`);
return (dispach) => {

fs.readFile(imagePath, 'base64')
    .then((data) => {
        //console.log(data);
        return Blob.build(data, { type: `${mime};BASE64` })
    })
    .then((blob) => {
        uploadBlob = blob;
        return imageRef.put(blob, { contentType: mime })
    })
.then(() => {
    uploadBlob.close()
    return imageRef.getDownloadURL()
})
.then((url) => {
    console.log("some url:",url)
    dispach({
            type: types.FIREBASE_REPORT_IMAGE_UPLOADED,
            payload:url
        })
})
.catch((error) => {
    console.log("Blob err: ",error);
    dispach({
            type: types.FIREBASE_REPORT_IMAGE_UPLOADED,
            payload:null
        })
});
}
答案

我通过生活“BLOB”并使用base64上传我的图像来修复它。像这样:

ref.putString(message, 'base64').then(function(snapshot) {

console.log('上传了一个base64字符串!'); });

工作如此顺利

以上是关于将映像上载到存储后的firebase更新数据库的主要内容,如果未能解决你的问题,请参考以下文章

读取 Firebase 存储映像安全规则

如何将firebase存储下载URL保存到firestore集合?

如何以BLOB格式将图像上传到Firebase存储?

Firebase 选项中未定义存储桶。将个人资料照片更新到 Firebase

从Google Cloud Firestore上载和下载文件

将一个用户的图像上传到 Firebase 存储时,数据库中每个用户的图像链接都会更新