OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can not assign value to object property inside fetch() method

  • Thread starter Thread starter Resident Evil
  • Start date Start date
R

Resident Evil

Guest
Code:
function FetchAppJsonFiles() {
  return new Promise((Resolve, Reject) => {
    fetch("Json/Lang.json")
      .then((Reasponse) => {
        if (!Reasponse.ok) {
          throw new Error("");
        }
        return Reasponse.json();
      })
      .then((LanguageStrings) => {
        AppObj["LanguageStrings"] = LanguageStrings;
        if (!AppObj["LanguageStrings"]) {
          throw new Error("");
        }
        console.log(`Json/Lang.json was fetched successfully proceeding to fetch Json/Theme.json`, LanguageStrings);
        return fetch("Json/Theme.json");
      })
      .then((Reasponse) => {
        if (!Reasponse.ok) {
          throw new Error("");
        }
        return Reasponse.json();
      })
      .then((ThemeObj) => {
        AppObj["ThemeObj"] = ThemeObj;
        if (!AppObj["ThemeObj"]) {
          throw new Error("");
        }
        console.log(`Json/Theme.json was fetched successfully proceeding to fetch Json/TextArray.json`, ThemeObj);
        return fetch("Json/TextArray.json");
      })
      .then((Reasponse) => {
        if (!Reasponse.ok) {
          throw new Error("");
        }
        return Reasponse.json();
      })
      .then((TextArray) => {
        AppObj["TextArray"] = TextArray;
        if (!AppObj["TextArray"]) {
          throw new Error("");
        }
        console.log(`Json/TextArray.json was fetched successfully proceeding to lunch the app`, TextArray);
        Resolve();
      })
      .catch((Error) => {
        Reject(Error);
      });
  });
}

I have these 3 JSON files named TextArray.json , Lang.json and Theme.json , I want to fetch them and assign them to their relevant property inside an object called AppObj after i made sure they are fetched and parsed. However despite the fact that the JSON files are parsed successfully and i can see them in all 3 console.logs they will not get assigned to AppObj properties , for example

Code:
AppObj["TextArray"] = TextArray

and the value of my properties stays null.

asking chatGPT and seeking help of programming groups on telegram

<pre class="lang-js prettyprint-override"><code>function FetchAppJsonFiles() {
return new Promise((Resolve, Reject) => {
fetch("Json/Lang.json")
.then((Reasponse) => {
if (!Reasponse.ok) {
throw new Error("");
}
return Reasponse.json();
})
.then((LanguageStrings) => {
AppObj["LanguageStrings"] = LanguageStrings;
if (!AppObj["LanguageStrings"]) {
throw new Error("");
}
console.log(`Json/Lang.json was fetched successfully proceeding to fetch Json/Theme.json`, LanguageStrings);
return fetch("Json/Theme.json");
})
.then((Reasponse) => {
if (!Reasponse.ok) {
throw new Error("");
}
return Reasponse.json();
})
.then((ThemeObj) => {
AppObj["ThemeObj"] = ThemeObj;
if (!AppObj["ThemeObj"]) {
throw new Error("");
}
console.log(`Json/Theme.json was fetched successfully proceeding to fetch Json/TextArray.json`, ThemeObj);
return fetch("Json/TextArray.json");
})
.then((Reasponse) => {
if (!Reasponse.ok) {
throw new Error("");
}
return Reasponse.json();
})
.then((TextArray) => {
AppObj["TextArray"] = TextArray;
if (!AppObj["TextArray"]) {
throw new Error("");
}
console.log(`Json/TextArray.json was fetched successfully proceeding to lunch the app`, TextArray);
Resolve();
})
.catch((Error) => {
Reject(Error);
});
});
}

</code></pre>
<p>I have these 3 JSON files named <strong>TextArray.json</strong> , <strong>Lang.json</strong> and <strong>Theme.json</strong> , I want to fetch them and assign them to their relevant property inside an object called <strong>AppObj</strong> after i made sure they are fetched and parsed.
However despite the fact that the <strong>JSON</strong> files are parsed successfully and i can see them in all 3 <strong>console.logs</strong> they will not get assigned to <strong>AppObj</strong> properties , for example</p>
<pre><code>AppObj["TextArray"] = TextArray
</code></pre>
<p>and the value of my properties stays <strong>null</strong>.</p>
<p>asking chatGPT and seeking help of programming groups on telegram</p>
 

Latest posts

Top