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

232
Vistas
Array is empty in the response even after pushing elements to it

I am creating backend APIs for a Twitter clone. The below API shows the profile of a user. I want to show the tweets of the people that this user is following. But in my response the tweets array is returning an empty array even though I have pushed the data into this array.

Can somebody help me understand why is the tweets array empty in the response?

app.get('/api/profile', auth, function (req, res) {


    var email1 = req.user.email;
    var followers_data = [];
    var tweets = [];

    follower.find({ emailoffollowee: email1 }, (err, results) => {
        if (err)
            console.log(err);
        else {
            results.map((d, k) => {
                followers_data.push(d.emailoffollower);
            })
            for (var i = 0; i < followers_data.length; i++) {
                Tweet.find({ author: followers_data[i] }, (err, results1) => {
                    if (err)
                        console.log(err);
                    else {
                        results1.map((d, k) => {
                            tweets.push(d);
                        })
                    }
                })
            }
            res.json({
                isAuth: true,
                id: req.user._id,
                email: req.user.email,
                name: req.user.firstname + req.user.lastname,
                followers: followers_data,
                tweet: tweets

            });
        }
    })
        .catch(error => {
            console.log(error);
        })
});```
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You Have Tweet.find() which is an asynchronous function, to resolve the problem I used async/await

app.get('/api/profile', auth, function (req, res) {


var email1 = req.user.email;
var followers_data = [];
var tweets = [];

follower.find({ emailoffollowee: email1 }, async (err, results) => {
    if (err)
        console.log(err);
    else {
        results.map((d, k) => {
            followers_data.push(d.emailoffollower);
        })
        for (var i = 0; i < followers_data.length; i++) {
          let results1 = await Tweet.find({ author: followers_data[i] });
                results1.map((d, k) => {
                    tweets.push(d);
                })
        }
        res.json({
            isAuth: true,
            id: req.user._id,
            email: req.user.email,
            name: req.user.firstname + req.user.lastname,
            followers: followers_data,
            tweet: tweets

        });
    }
})
    .catch(error => {
        console.log(error);
    })
});

I recommend you to read this article async/await

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