0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

He did const newPostKey = this.db() .ref() .child("newsletters") .push().key; there's a chaining. I guess that's not the issue.
It's not the issue. The parenthesis after this.db that makes it a function call is the issue, as I explained.
Aha, that makes sense 😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.