skip to Main Content

Following the Firebase Database Setup, I can’t Read/Write anything. I’ve been googling for hours trying different approaches and nothing is working. I’m using Discord.js so I’ll remove the stuff unrelated to Firebase. For now I’m just hard coding the values for testing purposes.

require("dotenv").config();

const { initializeApp } = require('firebase/app');
const { getDatabase, ref, set } = require('firebase/database');

const firebaseConfig = {
  apiKey: process.env.API_KEY,
  authDomain: process.env.AUTH_DOMAIN,
  projectId: process.env.PROJECT_ID,
  storageBucket: process.env.STORAGE_BUCKET,
  messagingSenderId: process.env.MESSAGING_SENDER_ID,
  appId: process.env.APP_ID,
  measurementId: process.env.MEASUREMENT_ID,
};

// Initialize firebase
const app = initializeApp(firebaseConfig);

// Initialize Realtime Database
const database = getDatabase(app);

function writeUserData(userId, name, email, imageUrl) {
  const db = getDatabase();
  set(ref(db, 'users/' + userId), {
    username: name,
    email: email,
    profile_picture : imageUrl
  });
}

client.on("ready", () => { // when bot goes online
  console.log("Ready!");
  writeUserData("UserId", "Terry", "[email protected]", "https://imgur.com/t/bees/3bt83")
});

All of my firebaseConfig info is correct. My Read/Write permissions are both true.

Any help is appreciated.

I’ve tried the official Firebase documentation as well as other forums regarding the same problem.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out I needed to use Admin access, it's weird I couldn't find any mention of people trying this anywhere online. For those who may be seeing this in the future and have the same problem, try using the admin sdk.


  2. From the code you provided, there are a few issues that could be causing the problem. Let’s go through them step by step:

    Firebase Initialization: You are initializing the Firebase app correctly, but when you call getDatabase(), you’re not passing the app instance as an argument. Modify the line const db = getDatabase(); to const db = getDatabase(app); to use the initialized app.

    Setting the User Data: In your writeUserData function, you’re trying to set the user data using ref(db, ‘users/’ + userId). However, you’ve already obtained the ref in the global scope using const database = getDatabase(app);. So you can directly use ref(database, ‘users/’ + userId) instead of creating a new db variable.

    Update the line as follows:

    set(ref(database, 'users/' + userId), {
      username: name,
      email: email,
      profile_picture: imageUrl
    });
    

    It seems like you haven’t defined the client object used in the client.on("ready", () => { … }); block. Make sure you have the necessary Discord.js setup and the client object is defined correctly.
    After making these changes, the code should look like this:

    require("dotenv").config();
    
    const { initializeApp } = require('firebase/app');
    const { getDatabase, ref, set } = require('firebase/database');
    const { Client } = require('discord.js');
    
    const firebaseConfig = {
      apiKey: process.env.API_KEY,
      authDomain: process.env.AUTH_DOMAIN,
      projectId: process.env.PROJECT_ID,
      storageBucket: process.env.STORAGE_BUCKET,
      messagingSenderId: process.env.MESSAGING_SENDER_ID,
      appId: process.env.APP_ID,
      measurementId: process.env.MEASUREMENT_ID,
    };
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    
    // Initialize Realtime Database
    const database = getDatabase(app);
    
    function writeUserData(userId, name, email, imageUrl) {
      set(ref(database, 'users/' + userId), {
        username: name,
        email: email,
        profile_picture: imageUrl
      });
    }
    
    const client = new Client();
    
    client.on("ready", () => {
      console.log("Ready!");
      writeUserData("UserId", "Terry", "[email protected]", "https://imgur.com/t/bees/3bt83");
    });
    

    client.login("YOUR_DISCORD_TOKEN"); // Replace with your Discord bot token
    Make sure you replace "YOUR_DISCORD_TOKEN" with your actual Discord bot token. If you’re still having issues, make sure your Firebase Realtime Database rules are properly set to allow read and write access. Double-check your environment variables as well to ensure they are correctly set.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search