D
D
depstor2020-09-08 01:36:32
React
depstor, 2020-09-08 01:36:32

Extract data from json file?

fetch('./data/db.json')
.then((res) => res.json())
.then(res => console.log(res)

Code example, how I'm trying to display all json, maybe it needs to be imported somehow, maybe tell me something else, it gives me an error json parse < 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-09-08
@Robur

fetch makes an http request to your server. For it to work, your server must serve this file at the requested url. the file is loaded after the application starts working in the browser.
Judging by the question, this is a dark forest for you, read it, figure it out.
To import, the file must be available at the build stage. then instead of fetch you write import. Then the data from the file is available in the application immediately and is included directly in the application code.
For this to work, you must have a build system set up and json file imports working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question