How to return a new array within the Response.json() in Node.js?

Asked

Viewed 116 times

1

I would like to know how to return a new object array in Response.json() to be visible in Inuit.

I am receiving a lot of information from an api and the goal is to return to my frontend only an array of objects within the Response.json() created by me.

So I want to store them within a variable.

But the way I did, when I make more than one request my array is duplicated, and I don’t want that to happen.

That is the code:

let name = "";
let profileImage = "";
let texto = "";
let name2 = "";
let profileImage2 = "";
let texto2 = "";
let arr0 = [];

    

app.get('/teste', (request, response) => {
    T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=******=1', (err, data, response) => {

        data.map(item => {
            profileImage = item.user.profile_image_url;
            name = item.user.name;
            texto = item.text;
        });
        
        arr0.push({name, texto, profileImage});

    });
    T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=u*******=1', (err, data, response) => {
    
        data.map(item => {
            name2 = item.user.name;
            texto2 = item.text;
            profileImage2 = item.user.profile_image_url;
        });
        
        arr0.push({name2, texto2, profileImage2});
    
    });    

    
    return response.json(arr0);
});

Note: I only sent the most relevant snippets of the code!

  • 1

    Dude, it’s gotten a little difficult to interpret code without the full context. For example, is this block of code a single function? What is the return you are getting in the arr0 variable? In this particular case I think your problem has nothing to do with the Node, okay? is more of a Javascript thing anyway. The default JS method for concatenating arrays is Concat.

  • @M.Bertolazo he messed up the variable names, but, boy, it’s the same thing

  • the two T.Get make the same insertions... by duplicating...

  • the only difference is that the duplicate has the name of the properties ending with '2'

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.