How to get data from wildcard in firebase cloud functions

你离开我真会死。 提交于 2020-07-29 06:08:34

问题


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

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

Any suggestions?


回答1:


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

And then inside the function you can make another Promise

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

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
     })

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




回答2:


In firestore you can use this.

exports.getUser = functions.firestore
    .document('users/{userId}').onWrite((change, context) => {
        let userId = context.params.userId;
        //...
    });



回答3:


You can find wildcard values like this

wildCardValue = context.params.wildCardName


来源:https://stackoverflow.com/questions/47889401/how-to-get-data-from-wildcard-in-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!