0

How can I store text file's content to string variable using fetch? I'm using React.js and my File's content is being read in console, but I cannot seem to store it.

Naturally, TextFile returns me undefined, but I wonder if there is a way to catch a promise and store it permanently in a string var? Thank you in advance; Marin

let TextFile;
    function getFile(){
      return fetch(myText)
        .then((response)=>response.text())
        .then((text)=>console.log(text))
        .then((text)=>TextFile=text)
    }
getFile();
console.log(TextFile);
Marna
  • 3
  • 1

1 Answers1

1

you can use response.text() method in fetchAPI

function getData() {
  var myRequest = new Request('abc.txt');
  fetch(myRequest).then(function(response) {
    return response.text().then(function(text) {
      myArticle.innerHTML = text;
    });
  });
}

for more ref please see this.

Negi Rox
  • 3,828
  • 1
  • 11
  • 18