0

I have a database (IDBStore) defined for the watch. I am trying to write its data into a file and clear the database. Please see the code below:

function writeDataLocally() {
    var database = getDatabase();
    var onsuccess = function(array){
        var documentsDir, newFile;
        tizen.filesystem.resolve("documents", onResolve, function(error) {
            console.log("Could not resolve documents folder.");
            console.log(error);
        });

        function onResolve(result) {
            newFilePath = "MyFolder";
            documentsDir = result;
            var newDir = documentsDir.createDirectory(newFilePath);
            console.log("New folder is created.");
            /* ^^^^^ I can see this log ^^^^^ */

            newFile = documentsDir.createFile(newFilePath + "/sensordata_" + new TimeStamp() + ".txt");
            /* But the following log is not displayed! */
            /* I think the error now is in here! */
            console.log("New file is created.");
        }
        if(newFile != null) {
            newFile.openStream("w", onOpenStream, function(error) {
                console.log("Could not create the file.");
                console(error);
            }, "UTF-8");

            function onOpenStream(fs) {
                console.log(JSON.stringify(array));
                fs.write(JSON.stringify(array));
                fs.close();
                console.log("Data is written into the file.");
            }
        }
    },
    onerror = function(error){
        console.log(error);
    };
    database.getAll(onsuccess, onerror);
}

I am not getting any logs, which says it cannot create the required directory. Can you please see the bugs in this simple function? It may be basic, but I am new to tizen. Thanks.



UPDATE
Now I am able to create my folder and file in a way I like using the following code:
function writeDataLocally() {
    var database = getDatabase();
    var onsuccess = function(array){
        var documentsDir, newFile;
        tizen.filesystem.resolve("documents", onResolve, function(error) {
            console.log("Could not resolve documents folder.");
            console.log(error);
        });

        function onResolve(result) {
            newFilePath = "MyFolder";
            documentsDir = result;
            var newDir = documentsDir.createDirectory(newFilePath);
            console.log("New folder is created.");
            d = new Date();
            newFile = newDir.createFile("sensordata_" + d.toDateString() + ".txt");
            console.log("New file is created.");

            if(newFile != null) {
            newFile.openStream("w", onOpenStream, function(error) {
                console.log("Could not create the file.");
                console(error);
            }, "UTF-8");

            function onOpenStream(fs) {
                console.log("File is opened and ready to write...");
                fs.write(JSON.stringify(array));
                fs.close();
                newFile = null;
                console.log("Data is written into the file");
            };
        }
    },
    onerror = function(error){
        console.log(error);
    };
    database.getAll(onsuccess, onerror);
}

See the changes for newDir and 'newFile`. But here is the situation now:
If the folder exists, nothing is going to happen! Once I delete myFolder from the device, both file and folder are created and data is written into the file, but only for the first time.
If there is already a myFolder folder in the device, no other file is created in that directory. Suggestions?

Matin Kh
  • 5,192
  • 6
  • 53
  • 77

1 Answers1

1

You need to add the following privileges in your app's config.xml file:

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

Then you will be able to create directory/file.

EDIT

I think there is a typo in your code.

documentsDir = result;
var newDir = docuemntsDir.createDirectory(...

It should be,

documentsDir = result;
var newDir = documentsDir.createDirectory(...

EDIT2

Try below two methods:

Method 1 -

newFile = documentsDir.createFile("sensordata.txt");

Method 2 -

var timestamp = new TimeStamp();
newFile = documentsDir.createFile(newFilePath + "sensordata_" + timestamp + ".txt");

If both method works, then take "Method 2" for your code.

EDIT3

The callback function onResolve(), initialize "newFile" var, and you are trying to access outside it where it might have not been initialized.

Put below code inside onResolve() function

if(newFile != null) {
            newFile.openStream("w", onOpenStream, function(error) {
                console.log("Could not create the file.");
                console(error);
            }, "UTF-8");

Also, there is typo in onResolve function, below code

newFile = newDir.createFile("sensordata_" + d.toDateString() + ".txt");

should be

newFile = documentsDir.createFile("sensordata_" + d.toDateString() + ".txt");

One more problem is there in your code, you can't pass a variable direclty to createDirectory

var newDir = documentsDir.createDirectory(newFilePath);

Kindly use the string as it is, i.e

var newDir = documentsDir.createDirectory("MyFolder");

EDIT4 Basically you need to first trying resolving your created folder i.e if it exists then no need to create a new folder.

Remove this code from onsuccess

var documentsDir, newFile;
        tizen.filesystem.resolve("documents", onResolve, function(error) {
            console.log("Could not resolve documents folder.");
            console.log(error);
        });

And add below code in its place.

            var documentsDir, newFile;
            tizen.filesystem.resolve("MyFolder", onMyFolderResolve, function(error) {
                console.log("Your MyFolder doesn't exists.");
                console.log("Now resolve documents folder");
                tizen.filesystem.resolve("documents", onResolve, function(error){console.log("Could not resolve documents folder");});
            });

      function onMyFolderResolve(result) {
           console.log("Your folder exists");
           myfolderDir = result;
           d = new Date();
           newFile = myfolderDir.createFile("sensordata_" + d.toDateString() + ".txt");
}
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
Shreeram K
  • 1,719
  • 13
  • 22
  • These privileges are already added to `config.xml`, so this cannot be the solution. Maybe a simple syntax error in my code? I am very new to programming in Tizen so that may be the reason. – Matin Kh Jun 04 '15 at 16:01
  • Thanks for the catch. Yes, now a folder is created, but there is no sign of the file inside it. Also, I cannot see the log after file creation command. Is everything OK with that part? – Matin Kh Jun 04 '15 at 16:50
  • 1
    I think still your code is not working because you have put an extra slash in below code just before "sensordata_". newFile = documentsDir.createFile(newFilePath + "/sensordata_" + new TimeStamp() + ".txt"); Check edited answer. – Shreeram K Jun 04 '15 at 17:50
  • Resolved creating new file problem, please see the update. But so far, I have been unable to write into the file. Is that part of my code, the `if(newFile != null) {` reachable at all? – Matin Kh Jun 04 '15 at 20:43
  • Your suggestion resolved the issue, and now some data is written into the file. But the problem is, this happens only once. Once the folder is created, no more files are going to be created and written! I have updated the question too. Do you have any idea why this is this way? – Matin Kh Jun 05 '15 at 14:57
  • For folders you need to use a try catch, if it exists why you want to create again – Shreeram K Jun 05 '15 at 15:02
  • No, this has to be this way, so every time a new file is created. That's why I needed time stamp appended to file name. – Matin Kh Jun 05 '15 at 15:03
  • Ok got it. Then keep file mode as "w".But you need to write a logic if folder exists don't create folder again... – Shreeram K Jun 05 '15 at 15:05
  • How can I check the condition? – Matin Kh Jun 05 '15 at 15:05
  • 1
    Check my edited answer. That should work. If any error is coming, then try to fix or paste here. If you are using tizen sdk, try running the application "Run as" - Web Simulator, which is very effective in tracking down the errors. Everytime you are keeping the logs after the statement when error will come. So,obvsly you won't come to know where/what is the issue. – Shreeram K Jun 05 '15 at 15:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79799/discussion-between-srkushwaha-and-matin-kh). – Shreeram K Jun 05 '15 at 15:28
  • Thanks for your help. All done and every part works correctly now. I will add the final solution to your answer for further reference. – Matin Kh Jun 07 '15 at 18:26