0

So I have this function that takes the contents of a .txt file and returns it as a string... or at least it should. Instead, what it returns is "undefined". However, the "console.log(text);" outputs the proper text, which makes me believe that I can't convert the contents to a string since it has line breaks in it, with different text on each line of the .txt file. How can I convert the output to a string so that I can use .split and convert it to an array (having each line of text in the notecard translate into a separate item in the array)? Please help!

<script>//_____Read_Text_File_____
function done()
{
    var fileContents = this.response;
    var text = fileContents;
    console.log(text);
    return text;
}

function readTxt(fileName) {
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.addEventListener("load", done, false);
    xmlhttp.open("GET",fileName,true);
    xmlhttp.send();
}
</script>
Jemin
  • 11
  • 1
  • 2
    The problem is not about the line breaks, it's about the fact that the return value of the `done()` function is not used, your `readTxt()` function will always return `undefined` because it has no `return` statement, and in any case the Ajax request is asynchronous so you need to restructure your code such that `readTxt()` either returns a promise or accepts a callback argument. – nnnnnn Aug 22 '17 at 03:31
  • You should show the code where you get `undefined`. – t.niese Aug 22 '17 at 03:33
  • @nnnnnn it is used in the addEventLister. @jemin try adding content type header `xmlhttp.setRequestHeader("Content-Type", "text/plain");` after open method – Frankusky Aug 22 '17 at 03:33
  • Funny, I honestly realized that as I was writing this and then forgot about it entirely after writing the rest haha Thank you! – Jemin Aug 22 '17 at 03:33
  • I just modified the content type to text/plain instead of application/json – Frankusky Aug 22 '17 at 03:34
  • 2
    @Frankusky - No it isn't. The `done()` function is provided as a load callback, but its return value will be ignored. – nnnnnn Aug 22 '17 at 03:34
  • Please, write a example of the input file text. – Rafael Marcos Aug 22 '17 at 03:35
  • 2
    It will be most likely a duplicate to [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – t.niese Aug 22 '17 at 03:35
  • Got it working now, thanks guys! – Jemin Aug 22 '17 at 03:42
  • @Jemin was it the async issue? If yes then it could be closed as that? – t.niese Aug 22 '17 at 04:01
  • @t.niese Not at all, the problem was that I was trying to get the data through return in the done() function, but obviously that doesn't work. What I did was replace the return with a function buildImageGrid(text); to actually build the grid of images now that I have the proper url's, opposed to before where i had buildImageGrid calling the readTxt() function from within. readTxt() is now called on page load. The text file contains all of my image directories, followed by the accompanying alt text on each consecutive line. Again though, thanks guys! – Jemin Aug 22 '17 at 13:19
  • @Jemin `[...]the problem was that I was trying to get the data through return in the done() function, but obviously that doesn't work. What I did was replace the return with a function buildImageGrid(text);[...]` well that is exactly an async issue, that you cannot return data form an async function. – t.niese Aug 22 '17 at 14:19
  • Possible duplicate of [How to return value from an asynchronous callback function?](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – t.niese Aug 22 '17 at 14:20

0 Answers0