如何从 Firebase 云函数中的通配符获取数据 [英] How to get data from wildcard in firebase cloud functions

查看:18
本文介绍了如何从 Firebase 云函数中的通配符获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在以下 ref('/users/{userId}/items/{newItem}') 上触发事件我想从通配符 userId

Assuming that I trigger events on the following ref('/users/{userId}/items/{newItem}') I want to get a value from inside the wildcard userId

我试过 var token = event.params.userId.token 但它返回未定义

I have tried var token = event.params.userId.token but it return to be undefined

有什么建议吗?

推荐答案

Token 不是 trigger 事件自动返回的参数.在触发器通配符中,仅返回与通配符直接对应的参数.所以如果你想使用,你需要做以下事情

Token is not a parameter returned automatically from trigger events. In trigger wild cards only parameters corresponding directly to the wildcard are returned. So You need to do the following if you want to use the

var userId = event.params.userId

然后在函数内部你可以再做一个Promise

And then inside the function you can make another Promise

return admin.database().ref(/users/userId/token).once('value').then(data => { 
//do something here
 })

这将帮助您获得令牌.或者,如果您想一步完成,您可以使用 userID 作为通配符尝试以下操作.

This will help you get the token. Or if you want to do it in one step, you can try the following with userID as wild card.

return admin.database().ref(/users/{userId}/token).once('value').then(data => { 
    //do something here
     })

但是在这里您将收到多个令牌,然后您必须遍历子代以决定哪个 userId 与您相关

But here you will receive multiple tokens and then you will have to iterate through the children to decide which userId is of relevance to you

这篇关于如何从 Firebase 云函数中的通配符获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆