如何使用 Cloud Functions for Firebase 访问 uid?

How to access uid using Cloud Functions for Firebase?

我想在节点环境中使用 javascript 访问用户的 uid 并创建另一个对访问值 "name" 的引用。我的代码和firebase数据结构如下

exports.getData = functions.database.ref('/Followers/{uid}/{uid}/name')
    .onWrite(event => {

      //Getting the name data value

      const name = event.data.val();

      return admin.database().ref('/Accessed Name').set(name);
    });

我的 Firebase 样本数据结构:

访问的数据"name"应该创建一个引用,如下例所示。

要从匹配的路径中获取通配符值,您可以使用事件参数:

exports.getData = functions.database.ref('/Followers/{uid1}/{uid2}/name')
    .onWrite(event => {

    const uid1 = event.params.uid1
    const uid2 = event.params.uid2

}

请注意,您必须在路径中为它们指定不同的名称才能获得不同的值。在你的例子中,你给了他们相同的名字。

有关详细信息,请参阅 the documentation for database triggers

云函数V1.0

要获得1.0版本的uid,您需要做以下事情:

exports.getData = functions.database.ref('/Followers/{uid1}/{uid2}/name')
.onWrite((change, context) => {

 const uid1 = context.params.uid1
 const uid2 = context.params.uid2

});

data参数表示触发函数的数据。

context 参数提供有关函数执行的信息。

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff