nodejs server restart and send the same request when calling appendfile

299 Views Asked by At

i am writing a program that send multiple request ti retrieve some data from an external api , it works fine , but when i try to add a function "appendFile" that save the data received in a json the server starts to behave strangely and restart every time ,and resend the same request again and again which wasn't thes case before i added the function , if you have any insights abozt this problem please help , thanks : enter image description here here is my code to send request and and save the data in a json

const streamActivity = (client) => (activityId) => {
    return new Promise((resolve, reject) => {
        client.streams.activity(
            {
                id: activityId,
                types:
                    "time,heartrate,velocity_smooth,altitude,distance,latlng,cadence,watts,temp,moving,grade_smooth,average_speed",
                resolution: "high",
            },
             function (err, payload,next) {

                    if (!err ) {



                    //save to json
              appendToFile1('activity-streams.json', payload)


                    



                } else {
                   reject(err)
                }
            }
        );
    });
     // setInterval(streamActivity,5000);

};

and this is the append file function :

function appendToFile1(file, payload) {
    fs.appendFile(file, JSON.stringify({payload}), (err) => {
        if (err) throw err
        console.log('Done writing') // Success
    })
}
0

There are 0 best solutions below