Conor Cahalane
Conor Cahalane

Reputation: 3

firebase.database() is not a function

Error message:

TypeError: _this.db is not a function

Code:

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);
  };

Upvotes: 0

Views: 923

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317342

You assigned this.db like this:

this.db = app.database();

But then you refer to it later like this:

const newPostKey = this.db()
    ...

The object returned from app.database() isn't a function, so you can't call it like a function as you are now. It's a Database object. Perhaps you can just get rid of those parenthesis to make your code work the way you intended.

Upvotes: 1

Related Questions