javascript - firebase.database() 不是函数

标签 javascript firebase firebase-realtime-database typeerror

错误消息:

TypeError: _this.db is not a function

代码:

import app from "firebase/app";
import "firebase/auth";
import "firebase/database";

const config = {
  apiKey: "some-api-key",
  authDomain: "myapp.firebaseapp.com",
  databaseURL: "https://myapp.firebaseio.com",
  projectId: "aofndiuf",
  storageBucket: "project-somenumber.appspot.com",
  messagingSenderId: "793813245724y6597"
};

class Firebase {
  constructor() {
    app.initializeApp(config);
    this.auth = app.auth();
    this.db = app.database();
  }
  //  Auth API

  doCreateNewsletter = (news, name, description, email) => {
    const newsletter = { news, name, description, email };
    const newPostKey = this.db()
      .ref()
      .child("newsletters")
      .push().key;
    return this.db
      .ref()
      .child("/newsletters/" + newPostKey)
      .set(newsletter);
  };

最佳答案

您像这样分配了 this.db:

this.db = app.database();

但是稍后你会这样引用它:

const newPostKey = this.db()
    ...

app.database()返回的对象不是一个函数,所以你不能像现在这样像函数一样调用它。这是Database目的。也许您可以去掉这些括号,使您的代码按照您预期的方式工作。

关于javascript - firebase.database() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992800/

相关文章:

java - 如何使用 getValue(Subclass.class) 在 Firebase 中反序列化子类

javascript - 为什么下面代码中的alert没有执行:?

swift - 当将值更改为从 firebase 中提取的数据时,变量将恢复为初始值。怎样才能让值(value)留下来?

javascript - 如何使用 Firebase 在 Ionic 3 中通过参数检索数据

android - Firebase 监听器对电池生命周期和 CPU 的影响

javascript - 重新连接到托管环境中的上一个 Puppeteer session

android - 如何使用 Firebase 和 Kotlin 在文本 (Jetpack Compose) 中显示当前用户名?

javascript - 如何在 vue.js 中使用 vue-3-socket.io 和组合 api?

javascript - 在显示上执行过渡效果 :none element with hack

javascript - AngularJS - 使用 ng-repeat 的动态指令