0

So one of the features of the bot I am working on is that I can be on discord 'discreetly', meaning that I can have the idle status but if a friend knows what command to call, they can actually check if I am there. So in my index file I am using module.exports to store the variable that will contain the info that I set. In the other file, I have an array of values that depending on the value from the variable, the bot will respond with one of the phrases from the array. The problem is that when using the variable, I get an undefined response. Any ideas what I am doing wrong? Important to note

I have checked to make sure by putting an actual number and have gotten the correct response so it is an issue with the exporting. I also have the correct file path. I have also assigned variable info a number and gotten the same result. Edit: tried using the filepath as part of the variable in the array like so and got the same error

//This got me a new result so progress.
const filepath = filepath;
console.log(filepath); //This gets me {}
message.reply(activity[info]); //undefined

//new attempt that failed
//paraphrasing the filepath assignment
const filepath = filepath;
activity[filepath.info]

//first attempt
//from index
    var info = message.content;
        module.exports = info;

//from the other file
    var activity = ["Ready to play","Chilling","Doing work","afk","can talk"];
        console.log(activity[info]);
          message.reply(activity[info]);
Beat.
  • 29
  • 6
  • What's `message.content` in your index file? The error isn't originating from the module itself but what you declare the `exports` property as. – slothiful Jul 16 '19 at 15:49
  • @slothiful it's a user message sent through the discord, I do have conditions to make sure that numeric values are being taken. I'm sending a 0 through these tests – Beat. Jul 16 '19 at 15:53
  • Result of `console.log(message.content)`? – slothiful Jul 16 '19 at 15:53
  • @slothiful 0. Also checked the console log for the value of index.info and that is also undefined so it seems like 0 is losing its identity when it goes to the other file. – Beat. Jul 16 '19 at 16:35
  • You're requiring the file, right? Since you're assigning the variable to `module.exports`, the result will be the returned value of `require(...)`. There is no property of the module's exports in this case. – slothiful Jul 17 '19 at 19:33
  • @slothiful Ahhh, so that actually helped alot because now the value of the variable I assigned the require statement is empty brackets. If I had to assume, does that mean that because the value being entered is zero it doesn't bother to put anything in? I feel like my assumption is wrong though. – Beat. Jul 17 '19 at 23:20

1 Answers1

0

how to get a variable from a file to another file in node.js

So this is the solution to my problem

//index
var info = message.content;

module.exports.info = message.content;

//other file
const filepath = filepath;
var activity = [array of different values];
message.reply(activity[index.info]);

Thank you slothiful for you time in trying to help me. I really appreciate it

Beat.
  • 29
  • 6