【问题标题】:how do I get the dataID when using cloud firestore triggers function?使用 Cloud Firestore 触发器功能时如何获取 dataID?
【发布时间】:2019-02-06 09:36:43
【问题描述】:

我在 Firestore 数据库中有一个这样的事件集合:

我想使用 Cloud Firestore 触发器。当用户参加一个活动时,该活动的容量将为-1,当该字段更新时我想自动更新另一个字段(“rankPoint”)+3

要实现这一点,我需要在文档更新时触发一个函数

从firestore文档来看,它会是这样的

exports.updateUser = functions.firestore
    .document('users/{userId}')
    .onUpdate((change, context) => {

      // Get an object representing the document
      // e.g. {'name': 'Marie', 'age': 66}
      const newValue = change.after.data();

      // ...or the previous value before this update
      const previousValue = change.before.data();

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

      // perform desired operations ...
    });

对于我来说,它应该是 'events/{eventId}' 对吗?但是如何在通配符中获取该 eventID?它来自客户端吗?我的意思是在 iOS/Android 中我会编写代码来更新,比如

db.collection("eventss").document("someEventIDHere").setData(data)

是来自客户端吗?

【问题讨论】:

  • 为什么需要使用云功能?,是什么阻止您在应用程序中这样做?
  • 我想学点新东西,Firestore 看起来真的很不错,如果我可以使用云功能扩展它会更好。如果我只在后台编写一个代码而不是在 ios 和 android 应用程序中编写一个代码,在客户端编写更少的代码,也许会很棒

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


【解决方案1】:

您的函数将仅交付与已更改的函数模式 (users/{userId}) 匹配的文档。在您查询之前,其他文档不可用。所以,如果你想从你的事件集合中获得一个文档,你将不得不编写一些代码来访问它,然后决定从那里做什么。

听起来您希望有一个 eventId 通配符,但实际上没有。只有您在函数中定义的 userId 通配符。其他值将需要从您在用户文档中可用的数据中得出。

【讨论】:

猜你喜欢
  • 2020-10-15
  • 2020-11-09
  • 2023-02-24
  • 1970-01-01
  • 2021-11-09
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多