Retrieve data from a file
By pascal ruffie
03 Jul 2019 11:39
English
5 Replies
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 on 03 07, 2019
You must log in to use this service. Log in now?
The tag you entered already exists.
Do you want to report this post as spam?
The post has been reported as spam.
cannot be empty.
Are you sure you want to cancel and return to the list?
The code has been copied to the clipboard.
Enter a title.