简体   繁体   English

使用 Firebase Cloud Function 增加新用户的实时数据库计数

[英]Increment Realtime Database count on new user with Firebase Cloud Function

* UPDATED: THIS WORKS. * 更新:这有效。 SEE ANSWER BELOW *见下面的答案*

I'm trying to write a Firebase Cloud Function that increments a Realtime Database /userCount value whenever a new user is created.我正在尝试编写一个 Firebase 云函数,它会在创建新用户时增加实时数据库/userCount值。

I've tried the following, but am getting "TypeError: userCountRef.transaction is not a function" in incrementCountOnNewUser .我尝试了以下操作,但在incrementCountOnNewUser收到“TypeError: userCountRef.transaction is not a function”。

Transactions are working for my other function incrementCountOnOpen when the value of garage is set to true , but the ref is derived from the after event object.garage的值设置为true ,事务正在为我的另一个函数incrementCountOnOpen工作,但ref是从after事件对象派生的。

Any suggestions on how to do this?关于如何做到这一点的任何建议?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

// const userCountRef = functions.database.ref("/userCount"); // does NOT work
const userCountRef = admin.database().ref('/userCount'); // THIS WORKS!

exports.incrementCountOnNewUser = functions.auth.user().onCreate((user) => {
  return userCountRef.transaction(count => count + 1);
});

exports.incrementCountOnOpen = functions.database.ref("/garage").onUpdate(({after}) => {
  const countRef = after.ref.parent.child('count');
  const newValue = after.val();

  return newValue
    ? countRef.transaction(count => count + 1)
    : null;
});

It turns out that the code above works!事实证明上面的代码有效! I had switched from the commented out code (which does NOT work).我已经从注释掉的代码(这不起作用)切换了。 I guess it didn't wait long enough for it propagate after I published, because I see it working now!我想它在我发布后没有等待足够长的时间来传播,因为我现在看到它正在运行!

Sorry for the confusion.对困惑感到抱歉。

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

相关问题 Firebase实时数据库中的云功能 - Cloud Function in Firebase Realtime Database Firebase实时数据库和云功能 - 基于数据节点递增计数器 - Firebase Realtime Database and Cloud Functions — increment counter based on data nodes 使用 vuejs 增加/减少 Function 和 Firebase 实时数据库 - Increment/Decrement Function with Firebase Realtime Database using vuejs 如果Firebase实时数据库中存在数据库节点,请使用云功能检查 - Checking with Cloud Function if a database node exists in Firebase Realtime Database 如何使用云功能将新数据推送到Firebase实时数据库中? - How to push new data into firebase realtime database using cloud functions? Firebase 实时数据库中的云函数 - Cloud Functions in Firebase Realtime Database 如何从云功能中止到Firebase实时数据库的创建? - How to abort creation into firebase realtime database from a cloud function? 使用 javascript 将 firebase 云 function 中的数组发送到实时数据库 - Send an array in firebase cloud function to the realtime database using javascript 如何使用云 function 获取 Firebase 中的实时数据库? - How to get Realtime Database in Firebase by using cloud function? 如何使用云功能将子级添加到Firebase实时数据库的列表中? - How to add child to a List in Firebase Realtime Database with a cloud function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM