Home > Blockchain >  MongoDB only using one entry - Discord.js
MongoDB only using one entry - Discord.js

Time:08-19

Currently I have a command of which checks someone else's balance with my sort of economy system. The issue is that it's only storing one users data - so when the database is empty and the bot goes to create a profile for a user that is the only profile ever created - for example when another member goes to check their balance then it won't show their own profile but it shows only the first person to create a profile's balance. I've tried everything - nothing works. Please help... Below is the command to check balance, my schema and the profile creating function.

  if (message.author.bot) return;
  let member = message.mentions.members.first();
  if (member) {
    if (message.content.match('!ponyo balance') && profileSchema.findOne({ memberId: member.id, guildId: member.guild.id })) {
      console.log('trying to execute balance.createBalance() with the user id: '   member.id)
      const profileBalance = await balance.createBalance(member);
      console.log(`profileBalance: ${profileBalance}`)
      await message.reply(`${message.mentions.members.first()} has ${profileBalance.coins} coins! :D`);
    }
  }
})

const Balance = require('./profileSchema')
const mongoose = require('mongoose')
//create profile thingy
async function createBalance(member) {
  if (Balance.findOne({ memberId: member.id })) {
    let balanceProfile = await Balance.findOne({ memberId: member.id })
    if (balanceProfile) {
      return balanceProfile;
    } else {


      balanceProfile = await new Balance({
        userID: member.id,
        serverID: member.guild.id
      });
      await balanceProfile.save().catch(err => console.log(err));
      console.log("returning: "   balanceProfile.toString());
      return balanceProfile;
    }}}
 

module.exports = { createBalance };
const mongoose = require('mongoose');

const profileSchema = new mongoose.Schema({
    userID: { type: String, require: true, unique: true},
    serverID: { type: String, require: true },
    coins: { type: Number, default: 100 },
    bank: { type: Number }

})

const model = mongoose.model('ProfileModels', profileSchema);

module.exports = model;

CodePudding user response:

There is no memberId in your profile Schema..

if (Balance.findOne({ memberId: member.id })) {

Maybe you are mistakenly put memberId instead of userId or

you have Separate Balance Scheme.. which is not imported Correctly..

  • Related