【发布时间】: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