0

Tell me please why i can't to get local data from json in axios. db.json is at the root of the project, but in the getEvents function it throws error 404.

Help me please

operation.js

import FetchClient from 'app/utils/FetchClient';
import IdsAndByIds from 'app/utils/IdsAndByIds';
import { eventsFetch, setEvents } from './actions';

export const getEvents = () => async (dispatch) => {
  try {
    const { data } = await FetchClient.get('./db.json');
    dispatch(setEvents(IdsAndByIds(data)));
    dispatch(eventsFetch(false));
  } catch (error) {
    console.log(error);
  }
};

FetchClient.js

import axios from 'axios';
import { URL_API } from 'app/config'; //localhost:3009

const FetchClient = () => {
  const defaultOptions = {
    baseURL: URL_API,
    method: 'get',
    headers: {
      'Content-Type': 'application/json'
    }
  };
  const instance = axios.create(defaultOptions);
  return instance;
};

export default FetchClient();

actions.js

import * as types from './types';

export const eventsFetch = value => ({
  type: types.FETCHING_EVENTS,
  payload: value
});

export const setEvents = ({ objById, arrayIds }) => ({
  type: types.SET_EVENTS,
  payload: {
    eventById: objById,
    eventsOrder: arrayIds
  }
});
enter image description here
  • You can't fetch a JSON file from the local file system using axios. – Diego Francisco Nov 20 '19 at 08:34
  • @DiegoFrancisco it seems like using a local URL, you can somehow, so I'm trying to use the localhost through url to get data through axios, isn’t it possible? – Alexey Alpatov Nov 20 '19 at 08:41
  • You should be able to do local fetching with axios. Double check the file path. Look at other SO threads: [1](https://stackoverflow.com/questions/52569968/unable-to-fetch-data-from-local-json-file-by-axios), [2](https://stackoverflow.com/questions/54224164/trouble-getting-local-json-data-with-axios) – Nicolae Olariu Nov 20 '19 at 08:43
  • "it throws error 404" — A 404 not found error seems pretty clear. We've no idea what your HTTP server configuration looks like so we can't tell you why the URL doesn't map on to the JSON data. – Quentin Nov 20 '19 at 08:44
  • `'Content-Type': 'application/json'` — You're making a GET request. There is no request body to describe the type of. This is nonsense. – Quentin Nov 20 '19 at 08:44
  • If you are doing it for testing purpose add `.json` file in your public folder. That way you can access it. Just tried https://codesandbox.io/s/withered-field-qnh1r. – Sandip Nirmal Nov 20 '19 at 08:46
  • @NicolaeOlariu I added a screenshot of the project structure db.json is at the root – Alexey Alpatov Nov 20 '19 at 08:48
  • I think it could work if you serve the file using a server, are you doing that? – Diego Francisco Nov 20 '19 at 08:52
  • @SandipNirmal transferred from the root to the public still does not find – Alexey Alpatov Nov 20 '19 at 08:52
  • @DiegoFrancisco no, I need to check the work for the test, then api will come here already – Alexey Alpatov Nov 20 '19 at 08:54
  • Maybe this question could help: https://stackoverflow.com/questions/51859358/how-to-read-json-file-with-fetch-in-javascript. – Diego Francisco Nov 20 '19 at 08:55
  • so how to fix it in my case? – Alexey Alpatov Nov 20 '19 at 09:45

0 Answers0