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

Firebase FireStore:使用addDoc()添加文档后如何获取文档快照?

Firebase 9 获取刚创建文档的快照方法

在Firebase 9模块化API中,addDoc()返回的文档引用不再自带get()方法,需要使用独立的getDoc()函数来获取文档快照,具体操作如下:

  • 首先确保已导入所需方法:
import { addDoc, getDoc } from "firebase/firestore";
  • 创建文档后,调用getDoc()传入返回的文档引用即可获取快照:
// 创建文档
const songRef = await addDoc(songsCollection, song);
// 获取刚创建的文档快照
const screenShot = await getDoc(songRef);
  • 后续可通过以下方式操作快照:
// 判断文档是否存在
if (screenShot.exists()) {
  // 获取文档数据
  console.log("文档内容:", screenShot.data());
} else {
  console.log("目标文档不存在");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:50:33