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

Firebase addDoc报错:预期类型'Na'却为自定义对象,文档添加失败

解决Firebase addDoc报"Expected type 'Na', but it was: a custom an object"错误

你遇到的错误:

Uncaught (in promise) FirebaseError: Expected type 'Na', but it was: a custom an object

这个错误的核心原因是传递给addDoc的集合引用不是Firebase Web客户端SDK v9的合法CollectionReference类型,结合你的代码场景,按以下步骤排查修复:

  • 检查Firestore初始化逻辑
    确保你用的是Web客户端SDK的初始化方式,绝对不能在前端使用Admin SDK(Admin SDK是给服务器/后端场景设计的):

    // 正确的客户端初始化(模块化v9)
    import { initializeApp } from "firebase/app";
    import { getFirestore, collection } from "firebase/firestore";
    
    const firebaseConfig = { /* 你的项目配置 */ };
    const app = initializeApp(firebaseConfig);
    const db = getFirestore(app);
    
  • 修正集合引用的创建
    你的portfolioItemsCol必须用客户端SDK的collection()方法生成,不能用Admin风格的写法:

    // 正确创建集合引用
    const portfolioItemsCol = collection(db, "portfolioItems");
    
  • 验证title变量的类型
    确保title是纯字符串类型,而非对象。如果是React状态变量,检查setTitle的赋值逻辑,比如有没有误把事件对象(如e.target)直接赋值给title,导致它变成了非字符串类型。

按以上步骤调整后,addDoc就能正常向Firestore添加包含图片URL的文档了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:25:25