简体   繁体   中英

module.exports variable producing undefined result

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]);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM