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

Firebase 9+Vue3中setDoc合并数据报错,求参数正确位置

解决Firestore setDoc合并数据时的TypeError错误
  • 错误原因:你将{ merge: true }错误地传递给了doc()函数,Firebase会把这个对象当作文档路径的一部分处理,导致解析时抛出n.indexOf is not a function错误。merge配置是setDoc()的参数,而非doc()的参数。

  • 修正后的代码:

setDoc(
  doc(db, "users", userStore.id, "profile_data", "user_profile"),
  {
    country: selectedCountry.value,
    year_born: selectedYearBorn.value,        
    gender: selectedGender.value,
    profile_complete: true
  },
  { merge: true } // 这个配置项要放在setDoc的第三个参数位置
).then(() => {
  console.log("success")
}).catch(() => {
  console.log("alert")
})
  • 说明:
    • doc()函数仅负责构建目标文档的引用,参数依次为数据库实例、集合/文档路径分段,不需要传入配置选项。
    • setDoc()的第三个参数是可选配置对象,merge: true在这里会告诉Firestore只更新传入的字段,保留文档中原有未被覆盖的字段,实现合并而非全量覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 07:50:28