Node/Firebase onCall 异步函数返回 [英] Node/Firebase onCall asynchronous function return

查看:15
本文介绍了Node/Firebase onCall 异步函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户在聊天客户端(网站)中键入消息.此消息将发送到在 firebase 上设置的云功能.然后云函数查询返回响应的第 3 方 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 运行来自 3rd 方 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 返回的数据被发送回客户端.

To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client.

因此,您只需返回一个承诺,然后使用来自 3rd 方 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)
      } 
    });
  });
});

这篇关于Node/Firebase onCall 异步函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆