【问题标题】:how to get id of created document cloud functions Typescript如何获取创建的文档云函数 Typescript 的 id
【发布时间】:2019-10-06 05:09:55
【问题描述】:

在监听新文档创建的函数中。你如何获得该文档的ID

export const createUser = functions.firestore
    .document('users/{userId}/messages/{messagesID}')
    .onCreate((snap, context) => {
      // Get an object representing the document
      // e.g. {'name': 'Marie', 'age': 66}
      const newValue = snap.data();

      // access a particular field as you would any JS property
      const name = newValue.name;

      // perform desired operations ...
    });

我猜它在 context 参数中。我只是没有时间访问它

【问题讨论】:

    标签: typescript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    请查看documentation for Firestore triggers,它解释了如何做你想做的事。特别注意using wildcards部分。

    函数定义中大括号中的通配符userId 和messagesID 可在给定的context 中访问。

    const userId = context.params.userId;
    const messagesID = context.params.messagesID;
    

    您还可以使用其id property 从其快照中获取文档的 ID:

    const id = snap.id;
    

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多