javascript - 为什么没有定义snap.data().id?

标签 javascript firebase google-cloud-firestore google-cloud-functions

我似乎无法获取我尝试检索的文档的 ID。

我在网上查看了很多示例,它们似乎都在做我正在做的事情。

exports.moveToProfile = functions.firestore
  .document("tempProfiles/{id}")
  .onCreate(async (snap, context) => {
    const id = snap.data().id;
    const displayName = snap.data().displayName;

    const profile = await db
      .collection("profiles")
      .doc(id)
      .set({
        displayName: displayName,
        points: 0
      });

    return profile;
  });

最佳答案

在您的代码中,dataDocumentSnapshot类型对象。从链接的 API 文档中可以看到,该对象表示的文档的 ID 是其 id property 。 data() 为您提供了它的所有字段(并且正式文档 ID 不是其中之一,除非您将其写为字段。)因此,您可以使用 data.id 获取 ID。

关于javascript - 为什么没有定义snap.data().id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403383/

相关文章:

ios - Firebase Crashlytics 控制台未显示崩溃

reactjs - 如何保护 Firebase 托管中的第三方 API key ?

php - Firebase PHP 数据提交 - arrayValue, mapValue

javascript - Firebase 中正确的数据结构

javascript - 从 ngxdaterangepicker 日历中以 Angular 获取选定日期范围之间的数组中的所有日期

javascript - 如何在导入的 typescript 类上调用静态方法?

javascript - DeclarativeContent - 等待显示页面操作

javascript - <div id ="fb-root"和 JS 的最佳位置在哪里?

javascript - 将 32 位无符号 "Real"数据类型(拆分为两个 16 位有符号字)转换为 javascript

javascript - Firebase 存储下载图像 CORS 错误 [JavaScript]