Home > front end >  Getting undefined in a function used in module.exports
Getting undefined in a function used in module.exports

Time:09-26

So, I'm trying to make a function which upon using gives a expected result like this

{
  id: 'ID_OF_THE_USER',
  username: 'USERNAME_OF_THE_USER',
  avatar: 'AVATAR_ID_OF_THE_USER',
  discriminator: 'THE_TAG',
  public_flags: SOME_NUMBER,
  banner: null,
  banner_color: 'A_HEXCODE',
  accent_color: SOME_NUMBER
}

However, I get the correct result when I use the function in my main.js file, but when I have the function in another file (for example: function.js) and use module.exports in that external file and use that function in the main.js file, I get undefined.

The Function which I am talking abt:

const Discord = require('discord.js');

const client = new Discord.Client();

client.login('MyBotToken');

client.api.users('SomeUserID').get().then((result) => { return result; });

My current function.js file:

const Discord = require('discord.js');

module.exports =
{
   user: function(UserID)
    {
       const client = new Discord.Client();

       client.login('MyBotToken');

       client.api.users(UserID).get().then((result) => { return result; });
    }
}

My current main.js file:

const details = require('./function');

const userInfo = details.user('SomeUserID');

console.log(userInfo);

Now when I run my main.js file, I get undefined. Any help will be appreciated !

My Node version: 14.16 My Discord.js version: 12.5.3

CodePudding user response:

You have two issues. The first is that your function user() does not have a return statement and thus does not return any value at all. The return result does not apply to the scope of user(), but only to the anonymous function passed to .then(). Then whole expression .then((result) => { return result; }) doesn't have any effect at all in your example since any returned value from a function passed to .then() of a promise would itself automatically be wrapped in a promise.

The second issue is that your code in main.js does not expect a promise. To "get" the value from the promise returned from the Discord API, you have to either use await in a context where that is possible (inside an async function or at the top level of a ES module) or using .then() do assign a function to handle the result when the promise resolves. You cannot get the value back to a "non-asynchronous" scope.

What you could to mitigate these issues, is to rewrite function.js to

const Discord = require('discord.js');

module.exports = {
  user: function(UserID) {
    const client = new Discord.Client();
    client.login('MyBotToken');
    return client.api.users(UserID).get();
  }
}

and main.js to

const details = require('./function');
details
  .user('SomeUserID')
  .then((userInfo) => { console.log(userInfo); });

If you instead were call your user() function inside an async function, you could simply do

const details = require('./function');
const userInfo = await details.user('SomeUserID');
console.log(userInfo);
  • Related