I'm a new JavaScript programmer facing a problem that I can't seem to solve. I'm trying to save data in a Cloud Firestore Database using Firebase and trying to save and take data from the database. I've successfully created saving data as per this code below:
db.collection("data").doc(message.author.id).set({
"userID": message.author.id,
"userName": message.author.tag,
"userWallet": 500,
"userItems": {
"DogeCoin": 0,
"TrollCoin": 0,
"TrollTrophy": 0
},
"lastClaim": 0,
"lastCrime": 0
}).then(() => {
message.channel.send(`${message.author.tag}, you have successfully created your account.`)
})
While the saving part works the getting data part doesn't, I've tried a few methods but it still doesn't work. Right now, this is what I've worked on.
let wallet;
let bank;
db.collection("data").doc(message.author.id).get().then((q) => {
if (q.exists){
wallet = q.data().userWallet;
} else if(!q.exists){
message.channel.send(`${message.author.tag}, please go create an account.`)
return;
}
})
let embed = new Discord.MessageEmbed()
.setTitle(`Balance for ${message.author.tag}`)
.setAuthor(`${client.user.tag}`)
.setColor("RANDOM")
.setDescription(`Created by ${client.user.tag}`)
.addFields(
{name: "Bank", value: `${wallet}`}
)
.setImage("https://c.tenor.com/iQ7AzP7EgSAAAAAM/noice.gif")
message.reply({embeds: [embed]})
return;
I want to able to get the data from Firebase and use it in my code by showing it in an embed. Your help in sincerely appreciated. Thank you.