0

Im tring to read a simple setting from a json file, the json is this :

{
  "Label": "some string here"
}

form my javascript part i do:

import settings from '../settings.json';

then:

  var settings= ()=> { 
      const headers = new Headers();
      const requestOptions = {
          method: 'GET',
          headers: { ...headers.authentication, ...headers.culture, 'ContentType':'application/json',
          };
    return fetch(`${settings.Label}`, requestOptions).then(() => {
           return response.text().then(text => {
                  const data = text ? text && JSON.parse(text) : {};
                  let token = response.headers.get('X-Token');
                  if (token) {
                      data.token = token;
                  }
                  if (!response.ok) {
                     // manage error here
                  }
                  return Promise.reject(error);
           }
          return data;
        })
      });
    };
    // use settings here

Despite my many searches and attempts im not very expert in javascript,i have tried in many ways before, but the my variable 'settings' is not contain nothing.

Max
  • 87
  • 4
  • 12
  • 1
    Two things. 1: fetch returns a Promise, not "the value". Just because you're returning the subsequent `then` doesn't mean your code's going to wait for that unless you use `await` (see the async/await docs on MDN for information on how to do that). 2: why are you using the same name for that import and your var? – Mike 'Pomax' Kamermans Jan 21 '20 at 15:56
  • Have you tried to remove the `.Label` from your fetch method and do ```fetch(`${settings}`,..``` instead? Secondly, you are using the name `settings` as the identifier for the `/settings.json`, (which makes `settings` global to that file` and you also named the function `settings` too. That's conflicting – orimdominic Jan 21 '20 at 15:56
  • Are you using webpack or babel? This may be of use... https://stackoverflow.com/questions/34944099/how-to-import-a-json-file-in-ecmascript-6 – Francis Leigh Jan 21 '20 at 15:57
  • 1
    change the name of the imported json `settings` or the `var settings`, cuz this might cause unexpected behaviors. – Mu-Majid Jan 21 '20 at 16:00

4 Answers4

0

I believe you need to add an export to your JSON file

export const settings = {
 "label": "some string here"
}
0

Not much information given here, but this probably has to do with transpiling your javascript. You can use:

const settings = require('../settings.json')

instead.

Sahar
  • 66
  • 4
0

try this answer https://stackoverflow.com/a/59844868/7701381

Also, change the name of the imported json settings or the var settings, cuz this might cause unexpected behaviors

Mu-Majid
  • 851
  • 1
  • 9
  • 16
0

I had completely wrong the approach, the file is already available and I don't have to request to download it from the server, I just have to return string, without use of fetch or other:

return (`${settings.Label}`

Sorry and thank a lot for the support

Max
  • 87
  • 4
  • 12