Hex
Hex

Reputation: 47

ReferenceError: Cannot access 'variable' before initialization

I'm currently not the most experienced in coding in Node.JS, this is in Discord.JS in something I made a while back. I recently came back to it, and found it to not be working! I'm fairly clueless on this subject and will accept any help possible! Thank you!

module.exports = {
name: 'setpoint',
description: "Sets the player's points to a certain amount",
execute(message, args){
    const config = require('../config.json');
    const mongoose = require('mongoose');
    const Discord = require('discord.js');
    const client = new Discord.Client();
    mongoose.connect(config.mongoPass, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    const Data = require('../models/data.js');
    if(!args[1]) return message.reply('Please specify a player.');
    if(!args[2]) return message.reply('Please specify an amount.');
    if(!message.member.hasPermission('MANAGE_MEMBERS')) return message.reply("Insignificant permissions.");
    let user = message.guild.member(message.mentions.users.first());
    Data.findOne({
        userID: user.id
    }, (err, data) =>{
        if(err) console.log(err);
        if(!data) {
            const newData = new Data({
                name: user.username,
                userID: user.id,
                points: parseInt(args[2])
            })
            newData.save().catch(err => console.log(err));
            message.reply(`Set ${args[1]}'s points to ${addPoints}.`);
            return;
        }
        let addPoints = parseInt(args[2]);
        data.points=addPoints;
        message.reply(`Set ${args[1]}'s points to ${addPoints}.`);
        data.save();
        return;
    })
}

}

Here's the error,

events.js:292
      throw er; // Unhandled 'error' event
      ^

ReferenceError: Cannot access 'addPoints' before initialization
    at /home/container/commands/setpoint.js:29:61
    at /home/container/node_modules/mongoose/lib/model.js:4875:16
    at /home/container/node_modules/mongoose/lib/model.js:4875:16
    at /home/container/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
    at /home/container/node_modules/mongoose/lib/model.js:4898:21
    at /home/container/node_modules/mongoose/lib/query.js:4447:11
    at /home/container/node_modules/kareem/index.js:136:16
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
    at /home/container/node_modules/mongoose/lib/model.js:4877:13
    at /home/container/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:79:11)

Upvotes: 2

Views: 6448

Answers (1)

Ezequiel S. Sandoval
Ezequiel S. Sandoval

Reputation: 170

Line where you declare let addPoints = parseInt(args[2]); should go before message.reply(`Set ${args[1]}'s points to ${addPoints}.`);

Declare it before your if(!data)

          if(Err) console.log(err);
          let addPoints = parseInt(args[2]); // <--- HERE
          if(!data) {
            const newData = new Data({
                name: user.username,
                userID: user.id,
                points: parseInt(args[2])
            })
            newData.save().catch(err => console.log(err));
            message.reply(`Set ${args[1]}'s points to ${addPoints}.`); //<--- HERE
            return;
        }
        data.points=addPoints;

Upvotes: 1

Related Questions