[英]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.