UID在实施Cloud Firestore中的存在时来自哪里?

huangapple go评论54阅读模式
英文:

Where does the UID come from when implementing presence in Cloud Firestore

问题

I searched for a long time on presence topic, and i found the following doc

https://firebase.google.com/docs/firestore/solutions/presence.

I followed the doc and made all the necessary settings, but there is something i did not understand it.

in previous link, Specifically on the .js code for Firebase function they written the following

// Create a new function which is triggered on changes to /status/{uid}
// Note: This is a Realtime Database trigger, not Firestore.
exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
async (change, context) => {...etc...}

i need to understand how they got the uid value in .ref('/status/{uid}')??

i know that status is parent for uid child but how they can access it's value within .ref('/status/{uid}')

I spent three hours reviewing the document, but I couldn't figure out how they got uid value.

英文:

I searched for a long time on presence topic, and i found the following doc

https://firebase.google.com/docs/firestore/solutions/presence.

I followed the doc and made all the necessary settings , but there is something i did not understand it .

in previous link, Specifically on the .js code for Firebase function they written the following

// Create a new function which is triggered on changes to /status/{uid}
// Note: This is a Realtime Database trigger, *not* Firestore.
exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
    async (change, context) => {...etc...}

i need to understand how they got the uid value in .ref('/status/{uid}')??

i know that status is parent for uid child but how they can access it's value within .ref('/status/{uid}')

I spent three hours reviewing the document, but I couldn't figure out how they got uid value

答案1

得分: 1

以下是翻译好的部分:

使用 Cloud Functions 和 Realtime Database 构建 Firestore 在线状态 解决方案指南中的第一个代码片段显示了将在线状态信息写入实时数据库的代码:

// 从 Firebase Authentication 获取当前用户的 UID。
var uid = firebase.auth().currentUser.uid;

// 创建对此用户特定状态节点的引用。
// 这是我们将存储有关在线/离线状态的数据的位置。
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid);
...

因此,此代码确定了已登录用户的 UID,然后将在线状态信息写入实时数据库中的路径 /status/$uid。然后,这将触发您包括的 Cloud Function,该函数会获取路径中的 UID 部分:

exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
  ...

因此,每当用户更新状态节点时,此 Cloud Function 就会触发,并获取客户端写入的 uid 值,以便将在线状态信息写入 Firestore 中的相同 UID。

英文:

The first code snippet in the solution guide on using Cloud Functions and Realtime Database to build presence on Firestore shows the code that writes the presence information to the Realtime Database:

// Fetch the current user's ID from Firebase Authentication.
var uid = firebase.auth().currentUser.uid;

// Create a reference to this user's specific status node.
// This is where we will store data about being online/offline.
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid);
...

So this code determines the UID of the user that is signed in, and then writes the presence information to the path /status/$uid in the Realtime Database. This then triggers the Cloud Function that you included, which picks the UID part of the path up here:

exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
  ...

So this Cloud Function triggers whenever a user updates the status node, and takes the uid value that the client has written, so that is can write the presence information under the same UID to Firestore.

huangapple
  • 本文由 发表于 2023年4月10日 23:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定