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

Firebase WriteBatch.set()传入无效数据问题排查求助

问题分析与解决

错误原因很明确:Firestore的WriteBatch.set()方法要求第二个参数必须是对象类型,但你传入的是字符串(网址)。

看你的代码逻辑:

  • test是包含网址字符串的数组,用Object.assign({}, test)转成对象后,obj1的结构是{0: "https://www.google.com/", 1: "https://www.google.com/about"}
  • 遍历obj1的key时,obj1[key]拿到的是单个网址字符串,直接传给batch.set()就触发了类型错误。

修正方案

把每个网址包装成对象再传入,比如定义一个字段(比如url)来存储网址:

const addQuotes = async () => {
  let test = ['https://www.google.com/', 'https://www.google.com/about']
  const batch = writeBatch(db);

  test.forEach(url => {
    var docRef = db.collection("col").doc(); //自动生成唯一ID
    batch.set(docRef, { url: url }); // 将网址包装成对象传入
  });

  await batch.commit();
  console.log("hello");
}

额外优化建议:

  1. 没必要用Object.assign把数组转成对象,直接遍历原数组更简洁
  2. 函数声明为async,用await替代.then()写法更清晰易读

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 02:06:00