-1

I would like to know how do I return a new array of objects in response.json () to be visible in insomnia.

I am receiving various information from an API and the goal is to return to my frontend just an array of objects within the response.json () created by me.

So I want to store them inside a variable.

This is the code:

    var T = new Twit({
        consumer_key: process.env.CONSUMER_K,
        consumer_secret: process.env.CONSUMER_S,
        access_token: process.env.ACCESS_T,
        access_token_secret: process.env.ACCESS_T_S,
        timeout_ms: 60 * 1000,
        strictSSL: true,
})

app.get('/teste', (request, response) => {

let obj = {};
let obj2 = {};

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

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

    setTimeout(() => {
      return response.json([obj, obj2]);
    }, 1000);
    
});

With the help of the people responsible for me, to be able to return a cleaner json, but now I would like to know how to implement aync / await in place of "setTimeout ()", as the function only returns an array when using the "dalay" of "setTimeout ()".

Thanks!

2 Answers2

0

The problem is your array variable is out of the route function, making it global which means that the previous array values are stored even when you create a new GET request. Hence you can either add the array variable inside the route function or you can empty the array as soon as the GET request is made.

An example:

  var T = new Twit({
            consumer_key: process.env.CONSUMER_K,
            consumer_secret: process.env.CONSUMER_S,
            access_token: process.env.ACCESS_T,
            access_token_secret: process.env.ACCESS_T_S,
            timeout_ms: 60 * 1000,
            strictSSL: true,
    })
    
    app.get('/teste', (request, response) => {
        var obj = {};
        T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=******=1', (err, data, response) => {
    
            data.map(item => {
                obj.name = item.user.name, 
                obj.texto = item.text 
                obj.profileImage = item.user.profile_image_url
            });
    
        });
        T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=u*******=1', (err, data, response) => {
        
            data.map(item => {
                obj.name2 = item.user.name, 
                obj.texto2 = item.text 
                obj.profileImage2 = item.user.profile_image_url
            });
        
        });    
    
        return response.json([obj]);
    });
Megh Agarwal
  • 216
  • 1
  • 7
  • Megh, thanks for your comment, but can you send me a example this your soluction? I`m a beginner in programming. – Ivo Junior Dec 17 '20 at 19:25
  • Your solution helped me, but I believe that due to the API delay, my response was coming empty, so I put a "setTimeout ()" at the end. I did it this way because I was unable to implement async / await. However it is running. I made a change to how my code is now. Please have a look. I am now trying to put an "async / await". – Ivo Junior Dec 18 '20 at 17:57
0

instead of pushing a new Object each time, just create and empty object and assign the value on the very same object.

var T = new Twit({
        consumer_key: process.env.CONSUMER_K,
        consumer_secret: process.env.CONSUMER_S,
        access_token: process.env.ACCESS_T,
        access_token_secret: process.env.ACCESS_T_S,
        timeout_ms: 60 * 1000,
        strictSSL: true,
})

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

        data.map(item => {
            const temp = {
                profileImage: item.user.profile_image_url,
                name: item.user.name,
                texto: item.text,
            };
            arr0.push(temp);
        });
    });
    T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=u*******=1', (err, data, response) => {
    
        data.map(item => {
            const temp = {
                profileImage2: item.user.profile_image_url,
                name2: item.user.name,
                texto2: item.text,
            };
            arr0.push(temp);
        });    
    });
    
    return response.json([temp]);
});
darkash
  • 159
  • 1
  • 9
  • Darkash, thanks for your comment. However, when I run this code, no visualization of insomnia appears just an empty array: [{}]. Using "debug", I see that the variables "name", "text" and "profileImage" are assigned their respective values, including being within the variable "temp". However, they are not returned in console.log () or in the insomnia preview. – Ivo Junior Dec 17 '20 at 19:21
  • @IvoJunior what is the connecting key in the response between first request and second request that we can infer to merge them? Also I think the better representation for your json would be a pair of object from both first and second request within the same index if we can't pull the connection between both of them. – darkash Dec 18 '20 at 04:47
  • Thank you for your help. Your solution to prepare me too. – Ivo Junior Dec 18 '20 at 18:00