使用Firebase云功能增加新用户的实时数据库数量

问题描述 投票:-2回答:1

*更新:这项工作。请看下面的答案*

我正在尝试编写Firebase云功能,每当创建新用户时,该功能会增加实时数据库/userCount值。

我尝试了以下内容,但在incrementCountOnNewUser中得到“TypeError:userCountRef.transaction不是函数”。

incrementCountOnOpen的值设置为garage时,事务正在为我的其他函数true工作,但ref是从after事件对象派生的。

有关如何做到这一点的任何建议?

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;
});
javascript firebase-realtime-database google-cloud-functions firebase-admin
1个回答
0
投票

事实证明上面的代码有效!我已经从注释掉的代码(不起作用)切换。我想它在我发表后没有等待足够长的时间传播,因为我看到它现在正在工作!

对困惑感到抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.