【问题标题】:How to handle Json Response of Async callable cloud function如何处理异步可调用云函数的 Json 响应
【发布时间】:2021-08-07 02:52:52
【问题描述】:

我有以下函数,试图获取 Stripe API 创建的 url Link 它应该返回以下对象

{
  "object": "login_link",
  "created": 1620954357,
  "url": "https://connect.stripe.com/express/VGAOul448UhS",
  "id": "lael_JTn3grKOB073gL"
}

Nodejs 可调用函数(在 Dart/Flutter 中调用)

exports.loginLink = functions.https.onCall(async (data, context) => {
  const accountId = data.id;
  console.log('this is accountId ---->' + accountId);
  const loginLink = await stripe.accounts.createLoginLink(
  accountId
).then(() => {
  console.log(data.id)
  console.log(loginLink);
  return loginLink;
});

})

并从 Flutter 调用如下

  Future<void> getUrl() async {
    HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('loginLink');
    dynamic results = await callable.call(<String, dynamic>{
    'id': 'acct_1IpttSQgX5lyYgEb',});
    print (results.data);
    String urlLink = results.data;
  }

Firebase 日志返回未处理的错误 ReferenceError:在初始化之前无法访问“loginLink” 在 /workspace/index.js:41:15

对获取 stripe 提供的 url 有任何帮助吗?

【问题讨论】:

    标签: node.js flutter google-cloud-functions stripe-payments


    【解决方案1】:

    为您的可调用函数尝试以下代码:

    exports.loginLink = functions.https.onCall(async (data, context) => {
      const accountId = data.id;
      console.log('this is accountId ---->' + accountId);
      const loginLink = await stripe.accounts.createLoginLink(accountId);
      return loginLink;
    })
    

    您的原始代码混合了async/await.then(),这并不理想。您原来的 return 语句仅从 then() 块返回,而不是顶级函数,因此最终实际上没有返回任何内容。

    【讨论】:

    • 是的,之前尝试过,日志返回 loginLink: Unhandled error ReferenceError: loginLink is not defined at /workspace/index.js:37:3 at func (/workspace/node_modules/firebase-functions/lib /providers/https.js:273:32) 在 processTicksAndRejections (internal/process/task_queues.js:97:5) 2021-05-17T21:51:48.947010099ZD loginLink:函数执行耗时 423 毫秒,完成状态码:500
    • 这不是我所期望的错误; loginLink 已定义...您能否分享导致该错误的确切代码并注意哪一行是第 37 行?
    • 没关系,在我这边,它可以将 loginLink 作为对象返回。我只需要了解如何在我的 dart Flutter 未来函数中检索它(我认为类似 snap.data['url'])
    • 如果你print(results) 你会得到什么?这是否提示您url 在哪里?
    • 它工作得很好,我以为我需要解析一个 Json 对象,但它返回了一个地图,对于其他用户,您只需使用 results.data['url'] 处理'HttpsCallableResult'对象.谢谢帮助
    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多