我目前正在制造一个不和谐的机器人与discord.js模块,我想集成火基实时数据库。
以下是所有已释放的代码文件。
这是两个用来存储数据的类。
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中的代码找不到数据并将其重置,所以数据将被删除。
,有人知道我如何解决这个问题吗?
发布于 2021-11-18 17:53:53
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);
}
https://stackoverflow.com/questions/70005296
复制相似问题