Home > Back-end >  Problems with firebase and node.js
Problems with firebase and node.js

Time:11-19

I am currently making a discord bot with the discord.js module and I want to integrate the firebase realtime database.

Here are all the code files realted.

These are two classes used to store data.

class User 
{
  constructor(userId, guildId)
  {
    this.userId = userId
    this.guildId = guildId
    this.blacklisted = false
    this.muted = false
    this.coins = 0
    this.bank = 0
    this.level = 0
    this.xp = 0
    this.xpTotal = 0
  }
}

module.exports = User
class Guild
{
  constructor(id) 
  {
    this.id = id
    
    this.welcomeEnabled = false
    this.welcomeChannelId = ""
    this.welcomeMessage = "Welcome to ${guildName} ${username}, we are now ${members} on the server."
    this.goodbyMessage = "Goodby ${username}, we are now ${members} on the server."
    
    this.helpEnabled = true

    this.administrationEnabled = false
    this.administrationPrefix = "!"
    this.administrationRoleId = ""
    this.administrationTicketCategoryId = ""
    this.adminitrationAdminRoleId = ""

    this.levelingEnabled = false
    this.levelingChannelId = ""
    this.levelingMessage = "${member}, you gained a level, you are now level : ${level} !"
    this.levelingMultiplier = 1

    this.economyEnabled = false

    this.memberCountEnabled = false
    this.memberCountChannelId = ""
  }
}

module.exports = Guild;

And these are the two main files : firebase.js and sendData.js

const admin = require('firebase-admin');

var serviceAccount = require("./admin.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://DATABASE_URL.DATABASE_REGION.firebasedatabase.app/"
});

const database = admin.database();
database.goOnline()
module.exports = database
const database = require('../firebase.js')
const User = require('../data/user.js')

module.exports = async (Discord, client, message) => {
  const guildID = message.guild.id;
  const userID = message.author.id;

  if (userID === client.user.id) return;

  let profileData = null
  await database.ref(`/Users/${userID}-${guildID}`).on('value', (snapshot) => {
    if (snapshot.val() == null) profileData = new User(userID, guildID)
    else profileData = snapshot.val()
  })
  
  let guildData = null
  await database.ref(`/Guilds/${guildID}`).on('value', (snapshot) => {
    guildData = snapshot.val()
  })

  await database.ref(`/Users/${userID}-${guildID}`).set(profileData)
  await database.ref(`/Guilds/${guildID}`).set(guildData)
}

I am using replit.com to host my files and code. My repl uses node version 12.16.1. One of the main problem is that I can see the data in the database but it is then being removed because the code in sendData.js cannot find it and resets it.

Does anyone know how I can fix this issue ?

CodePudding user response:

The Firebase Reference.on() method does not return a Promise. Thus, the code quickly returns and sets your refs to null, deleting your data.

Instead, you may want to use Reference.once(), which does return a Promise:

const database = require("../firebase.js"),
  User = require("../data/user.js");

module.exports = async (Discord, client, message) => {
  const guildID = message.guild.id,
    userID = message.author.id;

  if (userID === client.user.id) return;

  let profileData = null;
  await database.ref(`/Users/${userID}-${guildID}`).once("value").then((snapshot) => {
    if (snapshot.val() == null) profileData = new User(userID, guildID);
    else profileData = snapshot.val();
  });
  
  let guildData = null;
  await database.ref(`/Guilds/${guildID}`).once("value").then((snapshot) => {
    guildData = snapshot.val();
  });

  await database.ref(`/Users/${userID}-${guildID}`).set(profileData);
  await database.ref(`/Guilds/${guildID}`).set(guildData);
}
  • Related