简体   繁体   English

如何使用 firebase 云函数从 firebase-realtime-database 中获取数据?

[英]How to fetch data from firebase-realtime-database using firebase cloud functions?

So I want to fetch data from my firebase realtime database in a firebase cloud function.所以我想从 firebase 云 function 中的 firebase 实时数据库中获取数据。

My Database structure:我的数据库结构:

数据库

My code:我的代码:

let usernameFetched: string
const snapshot = await admin.database().ref(`/Agent/${userUID}/username`).once("value")
usernameFetched = snapshot.val().username
console.log(`${usernameFetched}`)

return res.status(200).send({ 
    method: 'sendMessage',
    chat_id,
    text: `Your username is ${usernameFetched}) 
    //This returns a message to user via a telegram bot.
}

I'm using typescript in VSCODE by the way.顺便说一句,我在VSCODE中使用typescript。

The output is always UNDEFINED. output 始终未定义。 Why?为什么? The path is correct.路径是正确的。 I tried all the ways from similar question but didn't helped me.我尝试了类似问题的所有方法,但没有帮助我。

The function is ASYNC and I have to use the fetched value in other fucntions so I don't want to use return statement here. function 是异步的,我必须在其他功能中使用获取的值,所以我不想在这里使用 return 语句。 Any way?反正?

Change this:改变这个:

usernameFetched = snapshot.val().username

into this:进入这个:

usernameFetched = snapshot.val()

In your path, you are already using ref(/Agent/${userUID}/username) the username node.在您的路径中,您已经在使用ref(/Agent/${userUID}/username) username节点。 Therefore all you have to do to get the value is call .val() on snapshot .因此,您只需在snapshot上调用.val()即可获取该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用云功能将新数据推送到Firebase实时数据库中? - How to push new data into firebase realtime database using cloud functions? $uid 节点如何处理 firebase-realtime-database 规则? - How does $uid node works on firebase-realtime-database rules? 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? Firebase 实时数据库中的云函数 - Cloud Functions in Firebase Realtime Database 如何使用Google Cloud功能查询Firebase实时数据库 - How to query Firebase Realtime database using Google Cloud functions 如何在 React js 中从 firebase 实时数据库中获取数据 - how to fetch data from firebase realtime database in react js 使用 Firebase 云函数将新数据写入 Firebase 实时数据库时动态分配键值 - Dynamically assign a key value when writing a new data to firebase realtime database using firebase cloud functions Firebase云功能-如何获取模型数据 - Firebase cloud functions - How to fetch the model data 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? 如何使用 firebase v9 从实时数据库中获取数据? - How can i fetch data from realtime database using firebase v9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM