我如何在firebase函数上获取父文档的ID

发布于 2025-02-10 22:26:57 字数 449 浏览 1 评论 0原文

详细信息收集中有doc文档,它是{id}文档的孩子,col> col collection中。

我要做的是,当用户更改col/{id}/defot/doc中的字段时,我想在col/{id}中更新字段。

exports.onChanged = functions.firestore
    .document('col/{id}/details/doc')
    .onWrite((change, context) => {

     .....

但是,我不知道如何在JavaScript中获取{ID}

是否可以将ID作为字符串获取?

There is doc document in details collection, and it is child of {id} document in col collection.

What I want to do is, when user changes fields in col/{id}/details/doc, I wanna update fields in col/{id}.

exports.onChanged = functions.firestore
    .document('col/{id}/details/doc')
    .onWrite((change, context) => {

     .....

However, I don't know how to get {id} in JavaScript.

Is there way to get id as String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2025-02-17 22:26:57

您可以使用 上下文目的以获取路径中所有通配符的值,如下所示:

exports.onChanged = functions.firestore
    .document('col/{id}/details/doc')
    .onWrite((change, context) => {
      console.log(context.params.id);
    })

You can use the context object to get values of all wildcards in your path as shown below:

exports.onChanged = functions.firestore
    .document('col/{id}/details/doc')
    .onWrite((change, context) => {
      console.log(context.params.id);
    })
九歌凝 2025-02-17 22:26:57

您可以通过调用.parent方法来接收文档的{id}。在您的情况下,这可能是两个父母,因为第一位父母是收藏品。

因此,要访问文档的{id}将是:

change.after.ref.parent.parent.id

希望它能有所帮助

You might be able to receive the {id} of the document by calling the .parent method. In your case it might be two parents up, since the first parent is the collection.

So to access the {id} of the document would be:

change.after.ref.parent.parent.id

Hope it helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文