【问题标题】:Call Firebase HTTPS callable cloud functions in React在 React 中调用 Firebase HTTPS 可调用云函数
【发布时间】:2020-11-24 21:09:48
【问题描述】:

所以,我正在开发一个使用 Firebase 来实现许多功能的 React 项目。 现在我正在尝试在其中使用一些 HTTPS 可调用函数。

但我导入“firebase/functions”模块的方式似乎不正确。它给了我这个错误:

TypeError: Cannot read property 'httpsCallable' of undefined

以下是我如何进行导入和设置:

import app from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/functions';

const config = {
   // the config info here
};

  class Firebase {
    constructor() {
      app.initializeApp(config);
      this.auth = app.auth();
      this.db = app.firestore();
      this.functions = app.functions();
    }

    // trying to call the function
    doCreatePlanner = this.functions.httpsCallable('createPlanner')

谁能指出我正确的方向?

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-functions


    【解决方案1】:

    您在构造函数中定义之前尝试访问this.functions。要摆脱错误消息,您可以将对 httpsCallable 的调用移动到构造函数中:

        constructor() {
          app.initializeApp(config);
          this.auth = app.auth();
          this.db = app.firestore();
          this.functions = app.functions();
          const doCreatePlanner = this.functions.httpsCallable('createPlanner')
        }
    

    这可能不是你想要做的,但无论如何,在你定义它之前你不能使用this.functions

    【讨论】:

    • 谢谢道格!你的建议对我有用。但我还是有点困惑。你能看看更新的问题吗?评论太长了
    • 如果您还有其他问题,应单独发布,而不是添加到原始问题中。改变问题的性质实际上会使给定的答案无效,并且对将来的其他人没有帮助。
    • 是的。我的错。我将创建另一个问题。
    猜你喜欢
    • 2021-01-20
    • 2020-04-18
    • 2021-09-13
    • 2019-01-18
    • 2020-05-17
    • 1970-01-01
    • 2020-02-04
    • 2020-07-07
    • 2020-01-28
    相关资源
    最近更新 更多