节点/Firebase onCall异步函数返回 [英] Node/Firebase onCall asynchronous function return
问题描述
用户在聊天客户端(网站)中输入消息.此消息将发送到在Firebase上设置的云功能.然后,云功能查询返回响应的第三方API.此响应需要发送回客户端以显示.
The user types a message into a chat client (a website). This message is sent through to a cloud function set up on firebase. The cloud function then queries a 3rd party API which returns a response. This response needs to be sent back to the client to be displayed.
所以基本上我的客户像这样调用云函数...
So basically my client calls a cloud function like so...
var submitMessage = firebase.functions().httpsCallable('submitMessage');
submitMessage({message: userMessage}).thenfunction(result) {
//Process result
});
我的云功能看起来像这样...
My cloud function looks like this...
exports.submitMessage = functions.https.onCall((data, context) => {
request({
url: URL,
method: "POST",
json: true,
body: queryJSON //A json variable I've built previously
}, function (error, response, body) {
//Processes the result (which is in the body of the return)
});
return {response: "Test return"};
});
我已经包含了请求包,API调用本身可以完美运行.我可以从请求的返回函数中将结果打印到控制台.但是,显然因为请求是异步的,所以我不能只创建一个全局变量并将结果主体分配给它.我已经看到,请求完成后就可以调用回调函数.但是,我需要以某种方式将其传递给云函数的返回值.简而言之,我需要这样做...
I have included the request package and the API call itself works perfectly. I can print the result to the console from within the return function of the request. However, obviously because the request is asynchronous I can't just create a global variable and assign the result body to it. I have seen that you can call a callback function once the request is finished. However, I need to somehow pass that through to the cloud function return value. So put simply, I need to do this...
exports.submitMessage = functions.https.onCall((data, context) => {
var gBody;
request({
url: URL,
method: "POST",
json: true,
body: queryJSON //A json variable I've built previously
}, function (error, response, body) {
gBody = body;
});
return gBody;
});
(Yes, I am aware of this post... How do I return the response from an asynchronous call? but yeah as I said I need the variable scope to be within the cloud function itself so that I am able to return the value back to the client. Either I don't understand the methods used in that post or it does not accomplish what I am asking)
推荐答案
最后一个代码段中的方法不起作用:到您的return gBody
运行来自第三方API的回调时,所以gBody
为空.
The approach in your last snippet can't work: by the time your return gBody
runs the callback from the 3rd party API hasn't been called yet, so gBody
is empty.
如Cloud Functions文档所述:
As the Cloud Functions documentation says:
要在异步操作后返回数据,请返回promise. Promise返回的数据将发送回客户端.
To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client.
因此,您只需返回一个承诺,然后再使用来自第三方API的数据来解决该承诺.
So you just return a promise, and then later resolve that promise with the data from the 3rd party API.
exports.submitMessage = functions.https.onCall((data, context) => {
return new Promise(function(resolve, reject) {
request({
url: URL,
method: "POST",
json: true,
body: queryJSON //A json variable I've built previously
}, function (error, response, body) {
if (error) {
reject(error);
}
else {
resolve(body)
}
});
});
});
这篇关于节点/Firebase onCall异步函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!