Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda