【问题标题】:Cannot save "custom blob" to Firestore无法将“自定义 blob”保存到 Firestore
【发布时间】:2018-12-07 14:55:40
【问题描述】:

在 javascript 中,我从数组中创建一个 blob,如下所示:

const a = new Blob([[1,2,3]]);

当我尝试像这样保存这个 blob 时:

this.afs.collection('events').doc(event.getID()).set({data: new Blob([[1,2,3]])}).then((some) => {
      debugger;
    })

我得到一个错误:

使用无效数据调用 DocumentReference.set()。不支持的字段值:自定义 Blob 对象(在字段数据中找到)

如何通过 JS 将 blob 保存到 Firestore?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您的 blob 必须是 firebase.firestore.Blob 类型。然后使用静态fromUint8Array

    var firebase = require('firebase');
    var app = firebase.initializeApp({ ... });
    ...
    const blob = firebase.firestore.Blob.fromUint8Array(new Uint8Array([1,2,3]));
    

    【讨论】:

    • 如何导入该 blob 类型?
    • 使用您的代码,我收到此错误:“未捕获的错误:函数 Blob.fromUint8Array() 要求其第一个参数是 Uint8Array,但它是:一个数组”我建议使用工作代码。 (firebase.firestore.Blob.fromUint8Array(new Uint8Array([1,2,3])))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2017-01-12
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2020-09-28
    相关资源
    最近更新 更多