Page 1 of 1

StrParse error

PostPosted: Fri Aug 19, 2016 1:40 am
by Ronnie
Hi,
I'm testing the ability to read the testfile.txt from a server. The [TestArray] successfully shows the result (content data). I wanted to split/parse the result into an array. However, there was an error in Console log, "TypeError: Object doesn't support property or method 'split' at StrParse (file://...neoapp.min.js:2:933) at..."


========
Button 1
========
BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'http://sampledomainname.com/testfile.txt', false);
req.send(null);
if (req.readyState==4);
$rootScope.TestVar = req.responseText;
$rootScope.TestArray = $rootScope.TestVar.split("\r\n");
EndJS

CreateArray [Items] ""
StrParse "[TestArray]" "~" [Items]


Any advice appreciated.
Thanks!

Re: StrParse error

PostPosted: Fri Aug 19, 2016 2:14 am
by luishp
At first sight it seem to me that this:
Code: Select all
if (req.readyState==4);
$rootScope.TestVar = req.responseText;
$rootScope.TestArray = $rootScope.TestVar.split("\r\n");


should be like this:
Code: Select all
if (req.readyState==4){
  $rootScope.TestVar = req.responseText;
  $rootScope.TestArray = $rootScope.TestVar.split("\r\n");
}


Also keep in mind that JavaScript actions are executed even if the previous one has not finished yet.
For that reason there are usually callback functions. Functions that are executed once a download or any other time consuming process has ended.

Try to use two buttons: one for loading the text file and the other one to split the content

Re: StrParse error

PostPosted: Fri Aug 19, 2016 7:53 am
by Ronnie
Hi Luishp,
Thanks. Will explore your suggestions. Am new to Javascript... But given time, I believe it should be easier.

Regards
Ronnie

Re: StrParse error

PostPosted: Sat Aug 20, 2016 2:04 am
by luishp
Ronnie try using JQuery (included in NeoAppBuilder) instead of plain JavaScript.
It simplifies a lot many tasks. Take a look here:

http://www.w3schools.com/jquery/jquery_ajax_load.asp