Firebase cloud function calling external API with Axios, and writting to Firestore executes intermitently

  axios, google-cloud-firestore, javascript

I would be glad if I can get some help with my first question. I am an Android developer and I am currently doing a project which requires Firebase cloud functions, which I am new to.
I am calling an external API with Axios, and saving the JSON response into multiple Firestore documents. 4 out of 5 times that I call the function, it executes without error, but it does not write to Firestore, and the 1 time it writes to Firestore, it takes as long as 8 to 12 seconds to write the data.

I read something about promises and I applied to the function(to the best of my knowledge) but the problem persists. Please let an expert tell me what I am doing wrong. Below is my cloud function:

const functions = require("firebase-functions");
const axios = require("axios");
const admin = require("firebase-admin");

const api_token = "---------";
const includes = "------------";
const url = "------------" + api_token + includes;

exports.liveScores = functions.https.onRequest((req, res) => {
    return axios.get(url)
        .then(response => {
            var data  = response.data.data;

            data.forEach(function(obj){
                admin.firestore().collection("fixtures").doc(obj.id.toString()).set({
                id : obj.id,
                league_id : obj.league_id,
                venue : obj.venue_id,
                localteam_id : obj.localteam_id,
                visitorteam_id : obj.visitorteam_id,
                round_id : obj.round_id,
                round_name : obj.round ?  obj.round.data.name : null,
                stage_id : obj.stage_id,
                localteam_score : obj.scores.localteam_score,
                visitorteam_score : obj.scores.visitorteam_score,
                status : obj.time.status,
                timestamp : new admin.firestore.Timestamp(obj.time.starting_at.timestamp, 0),
                real_date : obj.time.starting_at.date,
                minute : obj.time.minute,
                injury_time : obj.time.injury_time,
                localteam_formation : obj.formations.localteam_formation,
                visitorteam_formation : obj.formations.visitorteam_formation,
                visitorTeam : obj.visitorTeam.data.name,
                localTeam : obj.localTeam.data.name,
                local_team_logo : obj.localTeam.data.logo_path,
                visitor_team_logo : obj.visitorTeam.data.logo_path,
                season_id : obj.season_id,
                referee : obj.referee ? obj.referee.data.fullname : null,
                aggregate : obj.aggregate ? obj.aggregate.data.result : null,
                venue_name : obj.venue && obj.venue.data.name ? obj.venue.data.name : null,
                weather_type : obj.weather_report && obj.weather_report.type ? obj.weather_report.type : null,
                weather_icon : obj.weather_report && obj.weather_report.icon ? obj.weather_report.icon : null,
                temperature : obj.weather_report && obj.weather_report.temperature_celcius ?         obj.weather_report.temperature_celcius.temp : null
                }).then(() => {
                    console.log("Fixtures table complete...");
                })
                
                var no_of_events = obj.events.data.length;
                for(var i=0; i < no_of_events; i++ ){
                    admin.firestore().collection("events").doc(obj.events.data[i].fixture_id.toString()).collection("all_events").doc(obj.events.data[i].id.toString()).set({
                        type : obj.events.data[i].type,
                        team_id : obj.events.data[i].team_id,
                        fixture_id : obj.events.data[i].fixture_id,
                        var_result : obj.events.data[i].var_result,
                        player_id : obj.events.data[i].player_id,
                        player_name : obj.events.data[i].player_name,
                        related_player_id : obj.events.data[i].related_player_id,
                        related_player_name : obj.events.data[i].related_player_name,
                        minute : obj.events.data[i].minute,
                        reason : obj.events.data[i].reason,
                        id : obj.events.data[i].id
                    }).then(() => {
                        console.log("Events table complete...");
                    })
                }

                for(var y=0; y < obj.lineup.data.length; y++){
                    admin.firestore().collection("lineup").doc(obj.lineup.data[y].fixture_id.toString()).collection("lineup").doc(obj.lineup.data[y].player_id.toString()).set({
                        fixture_id : obj.lineup.data[y].fixture_id,
                        team_id : obj.lineup.data[y].team_id,
                        player_id : obj.lineup.data[y].player_id,
                        player_name : obj.lineup.data[y].player_name,
                        number : obj.lineup.data[y].number,
                        position : obj.lineup.data[y].position,
                        formation_position : obj.lineup.data[y].formation_position,
                        captain : obj.lineup.data[y].captain
                    }).then(() => {
                        console.log("Lineup table complete...");
                    })
                }

                for(var x=0; x < obj.bench.data.length; x++){
                    admin.firestore().collection("bench").doc(obj.bench.data[x].fixture_id.toString()).collection("bench").doc(obj.bench.data[x].player_id.toString()).set({
                        fixture_id : obj.bench.data[x].fixture_id,
                        team_id : obj.bench.data[x].team_id,
                        player_id : obj.bench.data[x].player_id,
                        player_name : obj.bench.data[x].player_name,
                        number : obj.bench.data[x].number,
                        position : obj.bench.data[x].position,
                        formation_position : obj.bench.data[x].formation_position,
                        captain : obj.bench.data[x].captain
                    }).then(() => {
                        console.log("Bench table complete...");
                    })
                }

                for(var c=0; c < obj.stats.data.length; c++){
                    admin.firestore().collection("stats").doc(obj.stats.data[c].fixture_id.toString()).collection("all_stats").doc(obj.stats.data[c].team_id.toString()).set({
                        fixture_id : obj.stats.data[c].fixture_id,
                        team_id : obj.stats.data[c].team_id,
                        total_shots : obj.stats.data[c].shots && obj.stats.data[c].shots.total ? obj.stats.data[c].shots.total : null,
                        ongoal : obj.stats.data[c].shots && obj.stats.data[c].shots.ongoal ? obj.stats.data[c].shots.ongoal : null,
                        total_passes : obj.stats.data[c].passes && obj.stats.data[c].passes.total ? obj.stats.data[c].passes.total : null,
                        attacks : obj.stats.data[c].attacks && obj.stats.data[c].attacks.attacks ? obj.stats.data[c].attacks.attacks : null,
                        fouls : obj.stats.data[c].fouls,
                        corners : obj.stats.data[c].corners,
                        offsides : obj.stats.data[c].offsides,
                        possessiontime : obj.stats.data[c].possessiontime,
                        yellowcards : obj.stats.data[c].yellowcards,
                        redcards : obj.stats.data[c].redcards,
                        injuries : obj.stats.data[c].injuries,
                        saves : obj.stats.data[c].saves,
                        tackles : obj.stats.data[c].tackles
                    }).then(() => {
                        console.log("Stats table complete...");
                    })
                }

            })

            return res.status(200).json(
                console.log("successful")
            )
        })
        .catch(err => {
            console.log(err);
            return res.status(500).json({
                error: err
            })
        })
});

I would appreciate any kind of help.
Thank you.

Source: Ask Javascript Questions

LEAVE A COMMENT