繁体   English   中英

如何从 firebase 云 function 返回数据并返回异步响应?

[英]How to return data from firebase cloud function and return async response?

怎么了,我想从 firebase function 数据返回。 我的 firebase function:

exports.genericEmail = functions.https.onCall(async (data, context) => {
  if (!context.auth && !context.auth.token.email) {
    throw new functions.https.HttpsError("failed-precondition", "Must be logged with an email address")
  }

  return "Hello"

})

我对 https 的要求:

const callFirebaseFunction = event => {
  const addMessage = httpsCallable(functions, 'genericEmail');
  addMessage()
    .then((result) => {
      console.log(result.data.output);
    }).catch((error) => {
      console.log(`error: ${JSON.stringify(error)}`);
    });
}

在 firebase 控制台中写入 function 已执行: Firebase 控制台我在 JS 控制台中收到undefined 同样作为 function async我想获得跟踪函数成功/失败的能力,我该怎么做?

您正在从云 Function 返回一个string ,因此result.data将是一个字符串,并尝试读取属性output将记录undefined.

尝试返回 object,如下所示:

exports.genericEmail = functions.https.onCall(async (data, context) => {
  // function logic ...

  return { output: "Hello" }
})

现在result.data.output应该在客户端记录Hello

暂无
暂无

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

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