问题描述
假设我在以下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云功能中从通配符获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!