【问题标题】:How can I pass data from one export function to another export function?如何将数据从一个导出函数传递到另一个导出函数?
【发布时间】:2021-12-15 19:44:22
【问题描述】:

我在云函数中有一个函数,它从我的颤振应用程序中选择用户电子邮件,我想将它传递给一个函数,该函数在从外部客户端发布时使用用户电子邮件创建一个 firestore 文档,以便我可以存储firestore 中特定用户文档中的数据。我是 javascript 新手,我正在使用云函数来运行我的 Flutter 应用程序的后端代码。

我尝试过使用全局变量,但我一直在 exports.mpesa 函数中获取未定义的存储值,但在 exports.getUser 函数中,它会更新。任何帮助将不胜感激。

这就是我的 index.js 文件的样子

const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
let loggedInUser;

function convertTZ(date, tzString) {
 return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));   
}

exports.getUser = functions.https.onCall((data) => {
 console.log(loggedInUser); 
 loggedInUser = data;
 console.log(loggedInUser);
});

exports.mpesa = functions.https.onRequest((request, response) => {
  try{
   let date_ob = Date();
   const data = request.body;
   const date = convertTZ(date_ob,"Africa/Nairobi");
   prettydata = JSON.parse(JSON.stringify(data));
   console.log("Mpesa info");
   console.log(loggedInUser);
   functions.logger.info(data);
   console.log("Done1");
   functions.logger.info(prettydata.Body.stkCallback.ResultCode);
   console.log("Done2");
   admin.firestore().collection('transactions').doc(`${date}`).set(data);
   admin.firestore().collection('transactions').doc(`${loggedInUser}`).set(data);
   console.log("Done3");
  }
  catch(e){
   functions.logger.info(e);
  }
});

我正在flutter上调用getUser函数,如下所示

await FirebaseFunctions.instance.httpsCallable('getUser').call(loggedInUser.email);

一个包(https://pub.dev/packages/mpesa_flutter_plugin)将数据发送到get.mpesa函数的回调URL

输出的日志如下所示

getUser
Function execution started
12:05:55.673 am
getUser
Callable request verification passed
12:05:55.674 am
getUser
undefined
12:05:55.674 am
getUser
edward@mmw.co.ke
12:05:55.755 am
getUser
Function execution took 1489 ms, finished with status code: 200
12:06:03.373 am
mpesa
Function execution started
12:06:03.593 am
mpesa
Mpesa info
12:06:03.673 am
mpesa
undefined
12:06:03.674 am
mpesa
{"Body":{"stkCallback":{"ResultCode":1032,"ResultDesc":"Request cancelled by user","MerchantRequestID":"22982-12102077-1","CheckoutRequestID":"ws_CO_011120210005554833"}}}
12:06:03.674 am
mpesa
Done1
12:06:03.674 am
mpesa
1032
12:06:03.674 am
mpesa
Done2
12:06:06.105 am
mpesa
Done3
12:07:03.375 am
mpesa
Function execution took 60003 ms, finished with status: 'timeout'

【问题讨论】:

  • 为什么不将数据传递给 MPESA 函数?
  • 通过手机支付平台对外激活。这是他们发送付款数据的回调。

标签: javascript node.js flutter google-cloud-functions


【解决方案1】:

您有多种选择如何进行此操作。你真的不能像那样简单地调用另一个云函数。

1.将您的代码逻辑与“云功能”分开
这意味着将您的代码放入单独的 JavaScript 函数中,然后从云函数中调用该代码。

function A(...){
//...
}

exports.getUser = functions.https.onCall((data) => {
  // ... call function A
  A(...)
});

function B(){
 // ...
}

exports.mpesa = functions.https.onRequest((request, response) => {
  // ... call function B
  B(...)
  // And in case we want to call the logic from function A we can again call it here
  A(...)
});

2。将云功能分开并触发另一个功能
您可以通过多种方式触发另一个云功能。如果你想把它作为 onRequest 函数,你可以从另一个函数调用函数 url。

// for example a GET function
fetch("https://us-east4-your-project-prod.cloudfunctions.net/myFunction?data=mydata")

或者你可以使用 POST 请求...

但在你的情况下,我只是将我的代码逻辑移动到单独的 JavaScript 函数中,并为该函数设置 2 个触发器。一个可能是外部触发器,第二个可能来自应用程序。两个云函数都会做一些“逻辑”,然后调用最终的 javascript 函数来保存数据。

【讨论】:

  • 我对云功能比较陌生,所以我使用了第一个,因为我不太了解第二个并使用了这两个功能。我仍然在日志中得到“未定义”。 mpesa 功能由移动货币平台外部启动。我启动的唯一功能是getUser
猜你喜欢
  • 2018-07-29
  • 1970-01-01
  • 2020-09-26
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多