【问题标题】:How to access uid using Cloud Functions for Firebase?如何使用 Cloud Functions for Firebase 访问 uid?
【发布时间】:2017-08-24 16:52:39
【问题描述】:

我想在节点环境中使用 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 示例数据结构:

访问的数据“名称”应该创建一个引用,如下例所示。

【问题讨论】:

  • 我不清楚你想在这里做什么。您是否只是想在数据库触发路径中的通配符中获取匹配的 uid 的值?
  • 是的,我想访问这两个 uid

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

云函数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

【讨论】:

  • 对我来说仍然不太有效......“TypeError:无法读取未定义的属性'uid1'”。我尝试了 context.params.uid1 和 change.params.uid1。有什么我做错了吗?
  • 你检查过这个stackoverflow.com/questions/49647170/…吗,参数只能使用“上下文”@user2091936访问
  • 完美!解决了我的问题。似乎我只是在推送函数,但没有更新也对新 API 进行更改的标头。现在都在工作。谢谢!
【解决方案2】:

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

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。

【讨论】:

  • 非常感谢
猜你喜欢
  • 2023-03-11
  • 2017-09-24
  • 2018-11-23
  • 1970-01-01
  • 2017-11-03
  • 2017-09-07
  • 2018-01-03
  • 2022-12-01
  • 2017-09-04
相关资源
最近更新 更多