简体   繁体   English

访问 doc.id in.onWrite firebase 云 function

[英]accessing doc.id in .onWrite firebase cloud function

I'm triggering a cloud function with onWrite.我正在使用 onWrite 触发云 function。 Within the onWrite function, I want to do something with the ref ID of the document that was written.在 onWrite function 中,我想对所写文档的 ref ID 做一些事情。 How can I grab this?我怎么能抓住这个? My current code looks something like this, but I don't know how to fill eventID.我当前的代码看起来像这样,但我不知道如何填写 eventID。

exports.syncEvents = functions.firestore
    .document('events/{eventID}')
    .onWrite((change, context) => {

admin.firestore().collection('users').doc(host).collection('events').doc(eventID).set({
            active: true
          })
        });

change.after is a DocumentSnapshot of the document that changed, after it was written. change.after是写入后更改的文档的DocumentSnapshot DocumentSnapshot has a property called id that contains the id of the document. DocumentSnapshot具有一个名为id的属性,其中包含文档的ID。 So change.after.id is the document id. 因此change.after.id是文档ID。

The context object contains the params of the query.上下文object 包含查询的参数。 In your example, the context object will look like this:在您的示例中,上下文 object 将如下所示:

{
 ...
 params: {
   eventID: "whatever-eventId-is"
 }
 ...
}

So you just call get it from the context:因此,您只需从上下文中调用 get 它:

const eventId = context.params.eventId;

Here is more about context: https://firebase.google.com/docs/reference/functions/cloud_functions.eventcontext以下是有关上下文的更多信息: https://firebase.google.com/docs/reference/functions/cloud_functions.eventcontext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM