You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore调用fromUint8Array报错:无法读取undefined的该属性

解决Firestore设置字节数组字段时的TypeError问题

问题原因

你使用的是Firebase 9+的模块化API(树摇式导入),但代码里沿用了旧版命名空间API的写法(firestore.Blob.fromUint8Array),而你并未导入firestore这个命名空间对象,因此触发Cannot read properties of undefined错误。

解决方案

1. 补充导入Blob类

在Firestore模块的导入语句中,添加Blob:

import { getFirestore, serverTimestamp, collection, query, where, runTransaction, orderBy, limit, getDocs, doc, setDoc, connectFirestoreEmulator, Blob } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";

2. 修改字节对象创建代码

直接调用Blob.fromUint8Array()方法,不再依赖firestore命名空间:

const bytes = [72, 73];
const test = Blob.fromUint8Array(new Uint8Array(bytes));

3. 写入Firestore文档

将生成的test对象作为字段值写入文档即可:

await setDoc(doc(db, "你的集合名", "你的文档ID"), {
  byteField: test
});

额外说明

Firebase 9+推荐使用模块化API,所有功能都需要通过导入对应的函数/类来使用,不再支持旧版firestore.xxx这种命名空间调用方式,确保所有用到的API都已正确导入即可避免类似错误。

内容的提问来源于stack exchange,提问作者mytreat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 23:20:17