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

Jam*_*med 1 javascript firebase firebase-realtime-database google-cloud-functions

我想在节点环境中使用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);
    });
Run Code Online (Sandbox Code Playgroud)

我的Firebase示例数据结构:

1

访问的数据"名称"应该创建一个参考,例如下面的例子.

访问的数据

Dou*_*son 15

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

请注意,您必须在路径中为它们指定不同的名称以获取不同的值.在您的示例中,您为它们指定了相同的名称.

有关更多信息,请参阅数据库触发器的文档.