如何在创建触发器文档ID时获取云函数 [英] How to get cloud function onCreate trigger doc id

查看:33
本文介绍了如何在创建触发器文档ID时获取云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取触发oncreate函数的文档的ID,但我尝试使用 snap.params.id ,如下所示,但是我不确定.

I want to get the id of the document that trigger the oncreate function but i tried snap.params.id like below but i am getting undefined.

当我在控制台上记录此 projectId 时,我变得未定义,有时会抛出错误,并且我使用的版本是"firebase-admin":〜5.12.1","firebase-functions";:"^ 1.0.3"

When i console log this projectId, i am getting undefined and sometimes throws error and i am using version "firebase-admin": "~5.12.1", "firebase-functions": "^1.0.3"

    exports.created = functions.firestore.document('projects/{projectId}')
      .onCreate(snap => {
    
        const projectId = snap.params.id;
 
        const project = snap.data();
        const notification = {
          title: `${project.title}`,
          projectId: `${projectId}`
        }
    
    });

那我该如何获取文件ID?

So how do i get the document id?

推荐答案

由于您使用的是Cloud Functions版本> = 1.0,因此 onCreate 的事件具有如下所示的两个参数,因此您应使用 context.params .

Since you use a Cloud Functions version >= 1.0, events for onCreate have two parameters as shown below, and you should use context.params.

exports.created = functions.firestore.document('projects/{projectId}')
   .onCreate((snap, context) => {

       const projectId = context.params.projectId;

       const project = snap.data();

       //......

   });

有关更多详细信息,请参见此文档项目.

See this doc item for more detail.

这篇关于如何在创建触发器文档ID时获取云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆