[英]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.