简体   繁体   English

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

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

What's up, I want return from firebase function data.怎么了,我想从 firebase function 数据返回。 My 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"

})

My request from https:我对 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)}`);
    });
}

In firebase console written that function was executed: Firebase console I receive in JS console undefined .在 firebase 控制台中写入 function 已执行: Firebase 控制台我在 JS 控制台中收到undefined Also as function async I wanna get ability to track function's succeed/fail, how can I do that?同样作为 function async我想获得跟踪函数成功/失败的能力,我该怎么做?

You are returning a string from the Cloud Function so result.data will be a string and trying to read the property output will log undefined.您正在从云 Function 返回一个string ,因此result.data将是一个字符串,并尝试读取属性output将记录undefined.

Try returning an object instead as shown below:尝试返回 object,如下所示:

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

  return { output: "Hello" }
})

Now result.data.output should log Hello on client side.现在result.data.output应该在客户端记录Hello

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

相关问题 异步 Firebase 云 Function 触发器 - 在 catch 块上返回什么? - Async Firebase Cloud Function trigger - What to return on catch block? 从 Firebase 可调用 function(云功能)返回 stream 的正确方法? - Proper way to return stream from Firebase callable function (Cloud function)? Firebase Cloud Functions - 如何调用另一个函数并返回对象 - Firebase Cloud Functions - How to call another function and return object Firebase 云function嵌套条件下如何满足返回promise? - How to satisfy requirement to return promise in Firebase cloud function with nested conditions? 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function Firebase 实时数据库 onCall function 不从 remove() 操作返回响应数据 object - Firebase realtime database onCall function does not return a response data object from remove() operation firebase 云 function 对文档执行多项操作时如何正确使用'return' - How to properly use 'return' in a firebase cloud function when performing multiple actions against documents 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function 在 Async Google Cloud Functions 中使用“return” - Using "return" in Async Google Cloud Functions Firebase - 返回 function 中 onSnapshot 事件的值 - Firebase - return the value from the onSnapshot event in function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM