Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda