0

Background: I have downloaded and stored some files on my phone. I want to retrieve these files one after another and parse them individually. The results from the parsing are used for further calculations.

Problem: The files are not getting extracted properly. So the parsing is also not happening properly. The code I have so far:

 var knownFiles=[];
 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
    function(fs){
      fs.root.getDirectory('Amazedata', null, 
        function(d){
          var dirReader = d.createReader();
          dirReader.readEntries(function(entries){
            if (!entries.length) {
              alert('FileSystem is Empty');
            }
            else{
              for(var i=0; i<entries.length;i++){
                knownFiles.push(entries[i].name);
                var file = entries[i].fullPath;
                //console.log(file);
                Papa.parse(file,{
                  header: true,
                  dynamicTyping: true,
                  complete: function(){
                    var totAmt =0;
                    for(var i=1;i<=arguments[0].data.length;i++){
                      if(!arguments[0].data[1][2]){
                        totAmt += arguments[0].data[i][28];
                      }
                    }
                    var payerObj={
                      'PayerAccountID' : arguments[0].data[1][1],
                      'PayerAccountName' : arguments[0].data[1][8],
                      'TotalAmount' : totAmt 
                    };

                    payerAccArr.push(payerObj);
                    $('#loading1').removeClass('show').addClass('hide');
                    $('#content-Container').removeClass('hide').addClass('show');
                  },
                  error: errorFn
                });
              }

              // for(var i=0;i<knownFiles.length;i++){

              // }
            }
          }, onError);
        }, onError );
  }, onRequestError);

How do I get the file for parsing? What am I missing? The error I am getting is TypeError: Unable to get reference for 1 I am using Papa.parse for parsing.

R3tep
  • 12,512
  • 10
  • 48
  • 75
Pallavi Prasad
  • 577
  • 2
  • 9
  • 28
  • When you say, "I have downloaded and stored some files on my phone," do you mean you have downloaded them to the browser's sandboxed file system? In short, do you understand that the file system API's fie system is not your actual file system? – apsillers Jul 02 '15 at 15:37
  • No. When I mean I have downloaded and stored files, I mean I have extracted the contents of files on a server and created respective files in a folder on my phone with these contents. I have used the `createWriter()` method of the File API to create and write the files. – Pallavi Prasad Jul 02 '15 at 15:49

1 Answers1

0

I was able to solve this problem using getFile and fileEntry.file methods. The code was edited as follows:

var knownFiles=[];

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
 function(fs){
   fs.root.getDirectory('Amazedata', null, 
     function(d){
       var dirReader = d.createReader();
       dirReader.readEntries(function(entries){
         if (!entries.length) {
           alert('FileSystem is Empty');
         }
         else{
           var dirLength = entries.length;
           for(var i=0; i<entries.length;i++){
             knownFiles.push(entries[i].name);
             var reqfile = entries[i].fullPath;
             d.getFile(reqfile,{},
               function(fileEntry){
                 fileEntry.file(function(file){
                   Papa.parse(file,{
                     header: true,
                     dynamicTyping: true,
                     complete: function(){
                       //console.log(arguments);
                        var parserObj = arguments[0];
                        var totAmt =0;
                        for(var j=0; j<parserObj.data.length; j++){
                          if(!parserObj.data[j].LinkedAccountId){
                             totAmt += parserObj.data[j].TotalCost ? parseInt(parserObj.data[j].TotalCost) : 0;
                          }
                        }
                        console.log('total amount', totAmt);
                        var payerObj={
                           'PayerAccountID' : parserObj.data[0].PayerAccountId,
                           'PayerAccountName' : parserObj.data[0].PayerAccountName,
                           'TotalAmount' : totAmt 
                          };
                          console.log('payerObj',payerObj);
                          payerAccArr.push(payerObj);
                          if(payerAccArr.length == dirLength)
                            break;
                        },
                        error: errorFn
                      });
                    });
                  });
                //console.log(file);
              }

fileEntry.file returns a file Object that can be used for parsing.

Pallavi Prasad
  • 577
  • 2
  • 9
  • 28