Almog
Almog

Reputation: 2837

Firebase Functions Cannot read property 'databaseURL'

I keep getting the following errors in my Firebase Functions log

TypeError: Cannot read property 'databaseURL' of undefined at repoManagerDatabaseFromApp

Here is my code not sure what I'm doing wrong

exports.dynamicMetaTagsUpdate = functions.https.onRequest(async (request, response) => {
  console.log("dynamicMetaTagsUpdate Called");

  const html = fs.readFileSync("./index.html", "utf8");
  const {id} = request.query;
  const botDetector = new BotDetector();
  const userAgent = request.headers["user-agent"].toString();
  const bot = botDetector.parse(userAgent);
  if (bot || DEBUG_BOT) {
    try {
      console.log("try");

      const dbRef = firebase.database().ref("https://wiijii-visualizations-default-rtdb.firebaseio.com/");
      dbRef.child("charts").child(id).get().then((snapshot) => {
        if (snapshot.exists()) {
          console.log(snapshot.val());
        } else {
          console.log("No data available");
        }
      }).catch((error) => {
        console.error(error);
      });

      return response.send(html);
    } catch (e) {
      console.log(e);
      return response.send(html);
    }
  }
  return response.send(html);
});

Upvotes: 0

Views: 213

Answers (2)

Dharmaraj
Dharmaraj

Reputation: 50910

Usually you would use Firebase Admin SDK in a Cloud function and not the client SDK. When you use admin.database(), it returns the Database service for the default app. That essentially means you will get database instance of the Firebase project you have deployed the Cloud function too.

import * as admin from "firebase-admin"

// Default app
admin.initializeApp()
const database = admin.database()

You need to pass any arguments in those methods only if you are using multiple Firebase projects (or Admin SDK instances) in a single Cloud function. You can find more about initializing multiple instances in documentation if needed.

Upvotes: 0

Almog
Almog

Reputation: 2837

I got working here is my code

console.log("try");
      const ref = admin.database().ref("charts");
      console.log("ref");
      console.log(ref);

      ref.child(id).get().then((snapshot) => {
        if (snapshot.exists()) {
          console.log(snapshot.val());
        } else {
          console.log("No data available");
        }
      }).catch((error) => {
        console.error(error);
      });

Upvotes: 1

Related Questions