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

268
Vistas
Why is this function returning undefined when I know that it is not?
function redditCall(subreddit) {
    https.get(`https://www.reddit.com/r/${subreddit}/top.json`, (resp) => {
        let data = " ";
        resp.on("data", (chunk) => {
            data += chunk;
        });
        resp.on("end", () => {
            let json = JSON.parse(data);
            
            if (json.message === "Not Found" || json.message) {
                console.log("That subreddit was not found!");
            } else {
                if (!json.data.children[0].data.secure_media || json.data.children[0].data.secure_media === null) {
                    let postAttachment = json.data.children[0].data.url_overridden_by_dest;
                    // console.log(`That post has an picture attachment: ${postAttachment}`);
                    console.log(postAttachment); // returns the correct link
                    return postAttachment; // Picture Post
                } else {
                    let postAttachment = json.data.children[0].data.secure_media.reddit_video.scrubber_media_url;
                    //console.log(`That post has a video attachment: ${postAttachment}`);
                    return postAttachment; // Video Post
                }
            }
        });
    });
}

As the title says for some reason this function is returning undefined when I have logged postAttachment to the console and it shows the correct string but for some reason when I return postAttachment is says undefined. I've tried doing .toString() and it just shows undefined when I return it when I know for a fact that it is not.

about 4 years ago · Juan Pablo Isaza
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