Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
discord.js and node.js module not exporting correctly? returned statement not working

So Im trying to get a couple messages returned from a specific channel to get info about 'clans' that were registered before. However I can fetch the channel and all and then I can filter and loop through the returned messages but I cannot return the messages that I fetched no matter what. No Error no nothing but if you call .getClans from another file it always returns undefined.

module.exports.getClans = (guild, channelid) => {
    var channel = guild.channels.cache.get(channelid);

    channel.messages.fetch({ limit: 10 }).then(messages => {
            messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);

            return messages
        }
    )
}

I have even done things such as

module.exports.getClans = (guild, channelid) => {
    var channel = guild.channels.cache.get(channelid);

    channel.messages.fetch({ limit: 10 }).then(messages => {
            messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);

            var returned = []

            messages.forEach((el) =>{
                returned.push(el)
            })

            return returned;
        }
    )
}

which I checked in the module function itself returned was defined but calling the function again returned nothing other than undefined. Please help I have no clue how to fix this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You aren't actually returning anything from the function, you are only returning from the .then(messages, ...). You will need to retrieve what is being fetched and then return.

module.exports.getClans = (guild, channelid) => {
     var channel = guild.channels.cache.get(channelid);
     
     //This return is returning from .getClans()
     return channel.messages.fetch({ limit: 10 }).then(messages => {
            messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);
            
            //This return is returning from inside the .then()
            return messages
        }
    )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!