Home > Enterprise >  How to get response from a dynamic amount of URL's and combine JSON response from all of them
How to get response from a dynamic amount of URL's and combine JSON response from all of them

Time:12-08

Currently I have a small JSON file with a list of URL's to fetch data from, this amount can change at any time, so I have built a small function to request from the URL's and log a JSON response from each of them, but currently I need to combine the responses from all of them and send them in a res.json format.

app.post('/api/:db/find', async (req, res) => {
    try {
        const db = req.params.db;
        databases.find({}, function(err, docs) {
            for (const val of docs) {
                var url = val.url   "/"   db   "/findmany"
                axios.post(url, req.body)
                    .then(function(response) {
                        var o1 = response.data
                        var obj = Object.assign(o1);
                        console.log(obj)
                        //res.json(obj.data);
                    })
                    .catch(function(error) {
                        console.log(error)
                        res.json({ success: false });
                    });
            }
        });
    } catch {
        console.log(error)
        res.json({ success: false });
    }
});

I was thinking that the code would look something like this (Pseudo code)

app.post('/api/:db/find', async (req, res) => {
    try {
        const db = req.params.db;
        dynamic(var)object
        databases.find({}, function(err, docs) {
            for (const val of docs) {
                var url = val.url   "/"   db   "/findmany"
                axios.post(url, req.body)
                    .then(function(response) {
                        add(response) to (var) object
                    })
                    .catch(function(error) {
                        console.log(error)
                        res.json({ success: false });
                    });
            }

        when function is finished (console.log[(var)]) with added JSON from all of the requests

        });
    } catch {
        console.log(error)
        res.json({ success: false });
    }
});

I have seen solutions where the existing JSON data is overwritten if both the Response and JSON Object have the same value, and I want to avoid that.

I have tried storing the response data inside a var, but it would get overwritten if the data inside the var and the response had the same value.

I have also tried adding them together separately, but that makes numerous arrays, and I don't want that.

CodePudding user response:

How you combine the json responses received completely depends on the information content of the response json. You can combine the responses as shown below or write custom logic to combine the jsons depending on actual responses and send the result.

app.post('/api/:db/find', async (req, res) => {
    try {
        const db = req.params.db;
        const JsonAggreagate = []
        databases.find({}, async function(err, docs) {
            try {
                for (const val of docs) {
                    var url = val.url   "/"   db   "/findmany"
                    let result = await axios.post(url, req.body)
                    //LOGIC FOR COMBINING JSON RESPONSES
                    JsonAggreagate.push({url: url, result : result})
                }    
            } catch (error) {
                console.log(error)
                res.json({ success: false });
            }    
 
        //when function is finished (console.log[(var)]) with added JSON from all of the requests
        res.json({ success: true, result : JsonAggreagate});

        });
    } catch {
        console.log(error)
        res.json({ success: false });
    }
});
  • Related