Hi, is there any protection on file reading?
The app will be hanged if file is not exist.
let utf8_read = fs.readFileSync("test.txt", "utf-8");
...
Unhandled Error
Couldn't find file: test.txt
the file state is not protected too:
let stats = fs.statSync("test.txt");
if (stats) {...}
...
Unhandled Error
Couldn't stat file:test.txt
Answered! Go to the Best Answer.
Best AnswerI got around this by putting the file access in a try-catch block. Not ideal, but works for now until they add support for some sort of API for checking if a file exists.
Thank you skyway!
It works!
//check file
console.log("we are to check setting file regardless");
var utf8_read;
try {
utf8_read = fs.readFileSync("mySettings.txt", "utf-8");
} catch(e) {
console.log('Oh snap we have an error: ', e);
fs.writeFileSync("mySettings.txt", "off", "utf-8");
}
Thank you skyway!
It works!
//check file
console.log("we are to check setting file regardless");
var utf8_read;
try {
utf8_read = fs.readFileSync("mySettings.txt", "utf-8");
} catch(e) {
console.log('Oh snap we have an error: ', e);
fs.writeFileSync("mySettings.txt", "off", "utf-8");
}
Best AnswerI'm having trouble with this method, my try doesn't seem to catch anything.
try {
let jsonObject = fs.readFileSync("save.txt", "json");
level = jsonObject.level;
} catch(e) {
level = 1;
}It always says "Unhandled Error: Couldn't find file: save.txt".
What have I done wrong?
Best Answer
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
We are aware of this issue and it will be fixed in a future update. This should not prevent the code from executing.
Best AnswerI created a function to test whether a filename exists, which takes the filename as a parameter:
function checkForFile(filename) {
var dirIter;
var listDir = fs.listDirSync("/private/data");
while((dirIter = listDir.next()) && !dirIter.done) {
if (dirIter.value === filename) {
return true;
}
}
return false;
}
Best Answer