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?