简体   繁体   English

如何在Firebase云功能之间传递数据

[英]How to pass data between firebase cloud functions

I'm trying to fetch the current user id from the app by making https requests to call functions from the client app. 我正在尝试通过发出https请求来从客户端应用程序调用函数来从应用程序中获取当前用户ID。 It's working and I'm fetching the uid. 它正在工作,我正在获取uid。 The problem is whenever the onWrite() function is triggered I need to send notifications to a specific user under a specific user id. 问题是无论何时触发onWrite()函数,我都需要在特定用户ID下向特定用户发送通知。 How can I pass the uid that I got from my https functions to the trigger functions? 如何将从https函数获得的uid传递给触发函数?

I just retrieved the user id of the current user from the client app using an https.onCall(). 我刚刚使用https.onCall()从客户端应用程序检索了当前用户的用户ID。 Then in the trigger function, I'm trying to send notifications to a specific user classified by uid using expo push notifications API. 然后在触发功能中,我尝试使用expo push notifications API将通知发送给按uid分类的特定用户。

functions/index.js 功能/ index.js

const functions = require('firebase-functions');
var fetch = require('node-fetch')

const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)


exports.getUid = functions.https.onCall((data, context) => {
    uid = data.text
    console.log(data.text)
})

exports.makerOrders = functions.database.ref('orders')
    .onWrite((snapShot, context) => {
        console.log('functions is triggered :)')

    return admin.database().ref('Notifications').child(uid)
        .once('value')
        .then((shot) => {
            var message = []

            var tokens = shot.val().expoTokens;

            if (tokens) {
                message.push({
                    "to": tokens,
                    "body": "Notifications are working fine :)"
                })
            }
            return Promise.all(message)
        }).then(message => {
            fetch('http://exp.host/--/api/v2/push/send', {
                method: "POST",
                headers: {
                    "Accept": "application/json",
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(message)
            })
            return Promise.all(message)
        })
})

App.js App.js

var uid = firebase.auth().currentUser.uid
var getUid = firebase.functions().httpsCallable('getUid')
getUid({text: uid}).then(result => {
  var msg = result.data
  console.log(msg)
  console.log('Called successfully :)')
}).catch(error => {
  console.log('Error :( in sending the requests')
});

I'm expecting to fetch the uid value of the current user from the app so that I can send push notifications to that specific user. 我希望从应用程序中获取当前用户的uid值,以便可以向该特定用户发送推送通知。

I can't able to retrieve the uid for the currently authed user. 我无法检索当前已认证用户的uid。

In order to pass data between functions there are two ways of doing this. 为了在函数之间传递数据,有两种方法。 You can use a function that it is triggered by HTTP request or a function that is triggered by Pub/Sub topic . 您可以使用由HTTP请求触发的功能,也可以使用由Pub / Sub topic触发的功能。

HTTP: HTTP:

  1. Create a Cloud Function with HTTP trigger 使用HTTP触发器创建云功能
  2. Open the details of the created function and under the Trigger tab you will find the URL that triggers the function. 打开创建的函数的详细信息,然后在“ Trigger选项卡下,找到触发函数的URL Use that URL to parse data from another function. 使用该URL解析来自另一个函数的数据。
  3. In the other function run a request using that URL and add at the end '?data=DATA_T0_SEND' 在另一个函数中,使用该URL运行请求,并在末尾添加“?data = DATA_T0_SEND”
  4. Catch the data from the second function using return request.args.get('data') 使用return request.args.get('data')从第二个函数捕获数据

Pub/Sub: 发布/订阅:

  1. Create a Cloud Function that triggers by a Pub/Sub topic. 创建由发布/订阅主题触发的云功能。
  2. On the other function use the Pub/Sub library to send the data to the topic 在另一个函数上,使用Pub / Sub库将数据发送到主题
  3. When the functions will be triggered with the Pub/Sub event get the data 当使用Pub / Sub事件触发功能时,获取数据
  4. Now process the data from that event 现在处理该事件的数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM