Languages

Menu
Sites
Language
Retrieve data from a file

Hello everybody, I am trying to reach a file I created into my wearable device (my problem isn't here) but I would like to gather some data which are inside of this file in order to reaffect them into variables but I didn't manage. How should I do ? Here is my code :

 

function getDetail(detail){
    var documentsDir;
    var dataToReturn = "init";
    tizen.filesystem.resolve("documents", function (dir) {
   documentsDir = dir;
               var file = documentsDir.resolve("myFile.json");
               file.openStream(
                    "r", 
                    function(fs) {
                        var text = fs.read(file.fileSize);       
                        fs.close();
                        var obj  = JSON.parse(text);
                        
                        switch (detail){
                        case "data1Value":
                        dataToReturn = obj.data1;
                        break;
                       
                        case "data2Value":
                        dataToReturn = obj.data2;
                        break;
                       
                        case "data3Value":
                        dataToReturn = obj.data3;
                        break;
                       
                        default:
                        dataToReturn = null;
                        }
                    alert(dataToReturn);
                    }, function (e) {
                        alert("Error " + e.message);
                    }, "UTF-8");
        });
        alert(dataToReturn);
return dataToReturn;
}

 

This function returns "init" instead of data1Value/data2Value/data3Value/null. Why ? Thank you in advance for your answers.

Edited by: pascal ruffie on 03 Jul, 2019

Responses

5 Replies
GEUNSOO KIM

bacause the callback function runs asynchronous with its host.

so getDetail() returns first, then your callback function (which is assigning "dataToReturn" value) runs.

If you want run it as you designed, you may need to use 'Promise' using polyfill.

(Tizen does not support 'Promise' yet. that's why you need 'Promise' polyfill)

pascal ruffie

Thanks a lot for your reply GEUNSOO KIM ! I will try this.

pascal ruffie

Well, it looks like it doesn't work :( , I integrated this into my code : <script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
found here : https://github.com/taylorhakes/promise-polyfill

But still my code doesn't work:

var dataToReturn = "init";
function getDetail(detail){
var documentsDir;
var prom = new Promise(function(resolve,reject){
    tizen.filesystem.resolve("documents", function (dir) {
   documentsDir = dir;
            var file = documentsDir.resolve("myFile.json");
            file.openStream(
                 "r", 
                 function(fs) {
                     var text = fs.read(file.fileSize);       
                     fs.close();
                     var obj  = JSON.parse(text);
                     
                     switch (detail){
                      case "data1Value":
                      dataToReturn = obj.data1;
                      break;
                     
                      case "data2Value":
                      dataToReturn = obj.data2;
                      break;
                     
                      case "data3Value":
                      dataToReturn = obj.data3;
                      break;
                     
                      default:
                      dataToReturn = null;
                     }
alert(dataToReturn);    //    appears ranked 2
                 }, function (e) {
                     alert("Error " + e.message);
                 }, "UTF-8");
     });
    if(true){
     resolve(dataToReturn);
    }
    else{
     reject(new Error ("It broke"));
    }
});
prom.then(function(result){
alert(result + " outside");//    appears ranked 1
});
    
// return dataToReturn;
}

Alexander Smirnov

How did you get the directory that way? Even on emulator (Tizen 4) and on actual watches I constantly got an error 'PLATFORM ERROR'

/sites/default/files/users/user-57807/clip2net_190704205012.png

I have these privileges:

<tizen:privilege name="http://tizen.org/privilege/content.read"/>

<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>

<tizen:privilege name="http://tizen.org/privilege/content.write"/>

<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>

So even in a simple Basic UI  with this simple resolve code don't want to work. Very strange.

pascal ruffie

Hi ! Try to add this privilege :
    <tizen:privilege name="http://tizen.org/privilege/mediastorage"/>