Nicolas Rosenkvist
Nicolas Rosenkvist

Reputation: 3

How do you get the JavaScript fetch() function to update to the new data being added to the file

I have a JSON file containing some data that i want to write to the screen. I have a function that contains a fetch() request that runs each time i click a button. This works great the first time i click the button as it takes the data and outputs it correctly. But once the JSON file gets new content added to it and i press the same button for a second time during the same session all the data is the exact same as the first time it was run. And i know for a fact that the code after the fetch() is ran (the console.log(data.lenght);). I've read a bunch about the Fetch API but i havent been able to find something to help with this. I've also read on another thread that the fetch() function only runs once but the code after always runs, seemingly confirming my problem.

function fetchJSON() {

    fetch("static/json/pool_values.json")
        .then(response => response.json())
        .then(data => {
            console.log(data.length);

Upvotes: 0

Views: 1609

Answers (1)

peinearydevelopment
peinearydevelopment

Reputation: 11464

It would seem as though there is an issue with your cache. This can be fixed by passing an option to your request.

fetch("static/json/pool_values.json", {cache: 'no-store'})
        .then(response => response.json())
        .then(data => {
            console.log(data.length);

Upvotes: 1

Related Questions