Search code examples
javascriptfileparsingpapaparse

Unable to get file for parsing


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.


Solution

  • 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.