firebase.database()不是函数

时间:2019-03-04 22:48:56

标签: 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);
  };

1 个答案:

答案 0 :(得分:1)

您这样分配了this.db

this.db = app.database();

但是您以后会像这样引用它:

const newPostKey = this.db()
    ...

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