如何处理 Json 异步可调用云函数的响应

How to handle Json Response of Async callable cloud function

我有以下功能,试图获取由 Stripe API 创建的 url Link 它应该return以下对象

{
  "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 日志 return 未处理的错误 ReferenceError:初始化前无法访问 'loginLink' 在 /workspace/index.js:41:15

对获得 stripe 提供的 url 有什么帮助吗?

为您的可调用函数尝试使用此代码:

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() 块返回,而不是顶级函数,因此最终没有返回任何内容。