首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >firebase和node.js的几个问题

firebase和node.js的几个问题
EN

Stack Overflow用户
提问于 2021-11-17 13:30:11
回答 1查看 53关注 0票数 0

我目前正在制造一个不和谐的机器人与discord.js模块,我想集成火基实时数据库。

以下是所有已释放的代码文件。

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

代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
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中的代码找不到数据并将其重置,所以数据将被删除。

,有人知道我如何解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-18 17:53:53

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

相反,您可能需要使用Reference.once(),它确实返回一个Promise

代码语言:javascript
运行
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70005296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档