2016-08-09 12 views
1

私は、Apache Cordovaのモバイルアプリケーションを使用してデータを収集し、Webサーバーに送信しています。私は、主な機能によってJavaScript関数(リライト)でSQLクエリから変数を取得する

がより集中するコードを書き直し

ことをやりたいとデータベースから明らか

//main function launched by a button 
function sendData(){ 
    //set all var as suggested 
    var content = ''; 
    var photoList = []; 
    var fileName = ''; 
    //call children functions 
    generateCsv(); // 1st child function 
    saveCSV();  // 2nd child function 
    uploadFile(); // 3rd child function 
} 

最初の関数の抽出データと上記VARに新しい値を設定します

function generateCsv() { // 1st child function 
    db.transaction(function(tx) { 
     tx.executeSql(selectAllStatement, [], function(tx, result) { 
      //do something I do not understand really... but work! 
      // but set value to var!! 
      console.log(content); //the new value is set correctly!! 
     }); 
    }); 
} 

私はメインの内側に次の関数の中で私の3つのvarの値をログに記録しようとする今、私はgenerateCsv機能によって行われた変更を受信しません...

function saveCSV() { // 2nd child function 
    console.log(fileName); //----not updated 
    console.log(content); //----not updated 
    console.log(photoList);//----not updated 
    //do something with my vars! 
} 

最後の子機能は

function uploadFile(){ 3rd child function 
    //do something and upload.... 
} 

コードの完全があるファイル

をアップロードします:

//------------------SEND DATA----------------->> 

function sendData(){ 
    var content = 'id,wateres,source,s_date,latitude,longitude,top,pump,pump_manual,other_pump,operator,operator_type,condition,disused,abandoned,access,water_presence,drinking_water,fee,fixme,note,photo,exif\n'; 
    var fileName = ''; 
    var date = new Date().toString(); 
    var photoList = []; 
    generateCsv(); 

} 


//---------------GENERATE CSV FILE--------------------->> 
function generateCsv() { 

    db.transaction(function(tx) { 
     tx.executeSql(selectAllStatement, [], function(tx, result) { 
      dataset = result.rows; 
      if (dataset.length!=0){ 

       for (var i = 0, item = null; i < dataset.length; i++) { 
        item = dataset.item(i); 

        //uploadPhoto(item['photo']); 
        photoList.push(item['photo']); 

        var exif = item['exif'].replace(/(\r\n|\n|\r)/gm, " | "); 
        exif = exif.replace(/,/g, '.'); 

        content += item['id']+ ',' 
          + item['wateres'] + ',' 
          + item['source'] + ',' 
          + item['s_date'] + ',' 
          + item['lat'] + ',' 
          + item['lon'] + ',' 
          + item['top'] + ',' 
          + item['pump'] + ',' 
          + item['pump_manual'] + ',' 
          + item['other_pump'] + ',' 
          + item['operator'] + ',' 
          + item['operator_type'] + ',' 
          + item['condition'] + ',' 
          + item['disused'] + ',' 
          + item['abandoned'] + ',' 
          + item['access'] + ',' 
          + item['water_presence'] + ',' 
          + item['drinking_water'] + ',' 
          + item['fee'] + ',' 
          + item['fixme'] + ',' 
          + item['note'] + ',' 
          + item['photo'] + ',' 
          + exif + '\n'; 

        fileName = item['source'] + '|' + date; 
       } 

       fileName = fileName.replace(/\ /g, "_"); 
       fileName = fileName.replace(/\,/g, ""); 
       fileName = fileName.replace(/\:/g, "-")+'.csv'; 

       $('#message').html('<p><strong>Data have been collected.</strong> </p>'); 

       saveCSV(); 
      } 
     }); 
    }); 
} 

//--------------------------SAVE CSV ------------------ 


function saveCSV() { 
    console.log(fileName); 

      var fileObject; 
      document.addEventListener("deviceready", onDeviceReady, true); 

      function onDeviceReady() { 
       window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, onFileSystemSuccess, fail); 
      } 

      function onFileSystemSuccess(fileSystem) { 
       fileSystem.root.getFile(fileName, { create: true, exclusive: false }, 
        gotFileEntry, fail); 
      } 

      function gotFileEntry(fileEntry) { 
       fileObject = fileEntry; 
       $('#saveFile_csv').on('click', function() { 
        saveFileContent(); 
        //uploadFile(fileName); 
       }); 
      } 

      function saveFileContent() { 
       fileObject.createWriter(gotFileWriter, fail); 
      } 

      function gotFileWriter(writer) { 
       var myText = document.getElementById('my_text').value + content; 
       writer.write(myText); 
       writer.onwriteend = function(evt) { 
        $('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>'); 
        var reader = new FileReader(); 
       }; 
      } 
      function fail(error) { 
       alert('fail to write file code = ' + error.code); 
      } 

} 

//----------------UPLOAD-CSV------------------------>> 
function uploadFile(fileName) { 

    var fileURL = "///storage/emulated/0/Android/data/com.app/cache/"+fileName; 

    function win(r) { 
     console.log("Code = " + r.responseCode); 
     console.log("Response = " + r.response); 
     console.log("Sent = " + r.bytesSent); 
     $('#contents').html('<strong>File uploaded</strong><br>'+ new Date()); 
    } 

    function fail(error) { 
     alert("An error has occurred uploading file: Code = " + error.code); 
     console.log("upload error source: " + error.source); 
     console.log("upload error target: " + error.target); 
    } 

    var uri = encodeURI("http://www.website.com/upload.php"); 

    var options = new FileUploadOptions(); 
    options.fileKey="file"; 
    options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1); 
    options.mimeType="text/plain"; 

    var headers={'headerParam':'headerValue'}; 

    options.headers = headers; 

    var ft = new FileTransfer(); 
    ft.upload(fileURL, uri, win, fail, options); 
    $('#contents').html('<strong>Whait upload confirmation...</strong>'); 
} 

をありがとう!

+0

whats issue?今すぐ –

+0

ありがとうChristohe! :) –

答えて

0

あなただけ

var content = ''; 
var fileName = ''; 
var date = new Date().toString(); 
var photoList = []; 

のように関数の外のすべての変数を設定する必要があり、変数に任意の値を割り当てるときに、あなたが今

function setVarItem() 
{ 
    content = "test string here or value here"; 
} 

のようにそのグローバル変数を使用する必要がありますその変数値を別の関数で取得します。

function getVarItem() 
{ 
    alert(content) //output: test string here or value here 
} 
+0

こんにちはKirankumar Dafdaとあなたの答えに感謝します。私が思う問題は、非同期ワークフローを含むgenerateCsv()関数内にあると思います... –

+0

すべての変数をローカルで1つの関数だけに書きましたので、その変数の値を別の関数に使うことはできません。値の異なる関数を割り当てて取得するだけです。 –

+0

こんにちはKirankumar Dafda、私はmain関数でvarを宣言していますが、まだ動作しません。おそらくgenerateCsv()の非同期関数に依存していますか?ありがとうございました ! –

関連する問題