【问题标题】:firebase update database after uploading image to storage将图像上传到存储后的firebase更新数据库
【发布时间】:2018-06-04 16:26:02
【问题描述】:

我正在使用 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
        })
});
}

【问题讨论】:

  • 上传完成到存储应该有回调。使用它将文件的 url 写入数据库。这是一种很常见的做法。有问题吗?你可以说得更详细点吗?也许显示你的代码?
  • 你在用firebase自带的SDK吗?
  • 我正在使用 firebase": "^4.6.0" "react-native-fetch-blob"

标签: reactjs firebase react-native upload google-cloud-firestore


【解决方案1】:

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

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

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

工作顺利

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 2017-05-31
    • 1970-01-01
    • 2018-11-22
    • 2020-08-09
    • 2017-09-25
    相关资源
    最近更新 更多