【问题标题】:Firebase functions onRequest to onCallFirebase 函数 onRequest 到 onCall
【发布时间】:2022-09-26 07:27:38
【问题描述】:

如何将此onRequest 函数传递给onCall?我正在使用模拟器在本地主机上工作。有人可以给我一些指导吗,我尝试按照functions.https.onCall 的文档进行操作,但我不明白我是否必须执行任何前面的步骤。

export const getFileInformation = functions.https.onRequest( (req, res) => {

  return cors( req, res, () => {

    const urls = [
      `url1`,
      `url2`,
      `url3`
    ];

    const urlsCalls: any[] = [];
    const resultados: any[] = [];

    urls.forEach( url => {
      urlsCalls.push(axios.get(url));
    });

    Promise.allSettled(urlsCalls)
    .then( response => {
      response.map( (element: any) => {
        const item = element.value.data;
        resultados.push(item);
      });
      console.log(resultados);
      res.json(resultados);
    })
    .catch( error => {
      console.log(error);
    });
  } );
});

我正在尝试这样简单的事情:

export const getFileInformation2 = functions.https.onCall( (data, context) => {
  return { msg: \'Hello from Firebase!\' };
});

但我收到以下错误:

{\"error\":{\"message\":\"Bad Request\",\"status\":\"INVALID_ARGUMENT\"}}

我应该如何处理onCall 函数?

  • 代码显示onRequest。你能展示你为onCall()尝试的代码吗?
  • @Dharmaraj 我已经编辑了我的问题,请看看我在尝试什么
  • 你怎么称呼onCall()云函数?你能分享你的客户端代码吗?我怀疑您没有使用 Firebase SDK 来调用它并且缺少一些必需的参数,就像在这篇文章中一样:Bad request returned from google cloud function
  • 我用模拟器http://localhost:5001/[myProjectName]/us-central1/getFileInformation2从我的本地主机调用它。从我读到的内容中,我必须包含一个标题和一些其他参数,请问你有什么具体的例子吗?
  • 你能用你用来调用函数的代码更新你的问题吗?也许是 Axios/fetch 请求,所以它可能更容易解释?

标签: javascript angular firebase google-cloud-functions


【解决方案1】:

HTTPS Callable 函数必须使用 POST 方法调用,Content-Type 必须为 application/json,并且正文必须包含一个名为 data 的字段,以便将数据传递给该方法。

见样本主体:

{
    "data": {
        "someKey": "someValue"
    }
}

将 URL 直接粘贴到浏览器时,会使用 GET 方法调用它,该方法会返回错误 Request has invalid method. GET,而您收到 {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}} 的原因是您没有传递必填字段 data请求正文。

您可以使用 curl 或 Postman 调用 https.onCall。 使用 curl 的示例调用:

curl -d '{"data": {"someKey": "someValue"}}' -H 'Content-Type: application/json' http://localhost:5001/[myProjectName]/us-central1/getFileInformation2

有关更多信息,您可以查看:

【讨论】:

  • 感谢@marc-anthony 的解释,我已经能够理解 onCall 函数的方法了。在这种情况下,我可以通过从前端调用得到响应:getInfo() { const functions = getFunctions(getApp()); connectFunctionsEmulator(functions, 'localhost', 5001); const getInfo = httpsCallable(functions, 'getFileInformation2') getInfo().then( (result) => { console.log(result.data); }) }
  • @PacoZevallos,您能否将其发布为答案,以便它也可以帮助社区。
【解决方案2】:

解决问题:

索引.ts

export const getFileInformation2 = functions.https.onCall( (data, context) => {

    const urls = [
      `url1`,
      `url2`,
      `url3`
    ];

    const urlsCalls: any[] = [];
    const resultados: any[] = [];

    urls.forEach( url => {
      return urlsCalls.push(axios.get(url));
    });

    return Promise.allSettled(urlsCalls)
    .then( response => {
      response.map( (element: any) => {
        const item = element.value.data;
        resultados.push(item);
      });
      console.log(resultados);
      return resultados
    })
    .catch( error => {
      console.log(error);
    });

});

Luego ejecuto 模拟器:

firebase emulators:start --only functions

Luego lo llamo desde mi frontEnd:

组件.ts

getFilesInformation() {
    const functions = getFunctions(getApp());
    connectFunctionsEmulator(functions, 'localhost', 5001);

    const getInfo = httpsCallable(functions, 'getFileInformation2')
    getInfo().then( (result: any) => {
      console.log(result.data);
    })
  }

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 2019-11-16
    • 1970-01-01
    • 2021-04-10
    • 2018-11-02
    • 2021-02-15
    • 2021-06-10
    • 2019-01-26
    • 2019-02-22
    相关资源
    最近更新 更多