【问题标题】:Problems with firebase and node.jsfirebase 和 node.js 的问题
【发布时间】:2021-12-28 11:36:57
【问题描述】:

我目前正在使用 discord.js 模块制作一个不和谐机器人,我想集成 firebase 实时数据库。

这里是所有的代码文件。

这是用于存储数据的两个类。

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;

这是两个主要文件:firebase.js 和 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)
}

我正在使用 replit.com 来托管我的文件和代码。我的 repl 使用节点版本 12.16.1。主要问题之一是我可以看到数据库中的数据,但由于 sendData.js 中的代码找不到并重置它,因此它被删除了。

有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: node.js class firebase-realtime-database discord.js


    【解决方案1】:

    Firebase Reference.on() 方法不返回 Promise。因此,代码会快速返回并将您的引用设置为null,从而删除您的数据。

    相反,您可能想要使用Reference.once(),它会返回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);
    }
    

    【讨论】:

    • 非常感谢 theusaf !!!!!!我只能找到 .on('value', (snapshot)... online
    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多