Jase
Jase

Reputation: 1105

Can't fetch local file

It works when I fetch a tsv file remotely:

// Works well
fetch(
    `https://raw.githubusercontent.com/reactivemarkets/react-financial-charts/master/packages/stories/src/data/MINUTES.tsv`,
)
    .then((response) => response.text())
    .then((data) => tsvParse(data, parseData()))
    .then((data) => {
        this.setState({
            data,
        });
    })
    .catch(() => {
        this.setState({
            message: `Failed to fetch data.`,
        });
    });

However, if I copy this file locally (both in the same directory as the .tsx file, as well as in the project's root directory), and replace the argument of the fetch function, it doesn't work.

// Doesn't work if I change the fetch input to this:
fetch('MINUTES.tsv')

// Also doesn't work
fetch('./MINUTES.tsv')

I get this error in the console output:

GET http://localhost:9000/minutes.tsv 404 (Not Found)

Upvotes: 0

Views: 3343

Answers (1)

Luka
Luka

Reputation: 1008

You should put your tsv file to the public folder of your project if you're using create-react-app for example. If you're not. You need to serve that file for it to be available for the fetch

Please, check this question/aswer

Upvotes: 1

Related Questions