fl384
fl384

Reputation: 13

discord bot not saving to mongoDB

I'm connecting to a mongoDB via a discordjs bot but the data/database doesn't seem to be saving. I have added the IP address and I have a console message that is showing up when the DB is connecting to confirm the connection.

basic setup of things:

lass EconomyClient extends Client {
  constructor() {
    super();
    this.discord = require("discord.js");
    this.fs = require("fs");
    this.path = require("path");
    this.ms = require("ms");
    this.mongoose = require("mongoose");
    this.commands = new Collection();
    this.timeouts = new Collection();
    this.config = {
      prefix: `h!`,
    };
    this.schema = this.mongoose.model(
      `economy`,
      new this.mongoose.Schema({
        User: String,
        Coins: Number
      })
    );
    const self = this;
    this.economy = {
        async getBal(User) {
            return await self.schema.findOne({ User }).then((d) => d ? d.Coins : 0);
        },
        async addBal(User, Coins) {
            return await self.schema.findOne({ User }), async(err, data) => {
              if(err) throw err;
              if(data) {
                  data.Coins += Coins;
              } else {
                  data = new self.schema({ User, Coins });
              };
              data.save();
            }
        }
    }
  }

and the connection...

start(token, path) {
    this.commandHandler(path);
    this.login(token);
    this.mongoose.connect(
      `mongodb+srv://<dbUser>:<dbPassword>@cluster0.3esa7.mongodb.net/Data?retryWrites=true&w=majority`,
      {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      }
    );
    this.mongoose.connection.on("connected", () =>
      console.log("DB connected ")
    );
    this.on("ready", () => console.log(`${this.user.tag} is now online!`));
    this.on("message", async (message) => {
      if (
        message.author.bot ||
        !message.guild ||
        !message.content.toLowerCase().startsWith(this.config.prefix)
      )
        return;

I've created a'work' command to get a random amount of coins and a 'getBalance' command. The work command seems to work - at the very least it picks a random number - but it doesn't seem to save it to the DB as the getBalance command always comes back as zero. When I go to the collectios tab in my mongoDB account it doesn't list anything (assuming it should?).

Upvotes: 0

Views: 1169

Answers (1)

fl384
fl384

Reputation: 13

There was a parentheses in the wrong spot --

was:

async addBal(User, Coins) {
            return await self.schema.findOne({ User }), async(err, data) => {
              if(err) throw err;
              if(data) {
                  data.Coins += Coins;
              } else {
                  data = new self.schema({ User, Coins });
              };
              data.save();
            }

notice the last paren here: self.schema.findOne({ User })

should have been:

async addBal(User, Coins) {
            return await self.schema.findOne({ User }, async(err, data) => {
              if(err) throw err;
              if(data) {
                  data.Coins += Coins;
              } else {
                  data = new self.schema({ User, Coins });
              };
              data.save();
            })

moved the paren to the end of the snippet.

Upvotes: 1

Related Questions