11-18-2018 06:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

11-18-2018 06:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Im trying to read the files in my app's private/data folder when the app loads
I used the code from the example in the guide: https://dev.fitbit.com/build/guides/file-system/#listing-files
In my app/index.js I do :
const listDir = fs.listDirSync("private/data");
When I run over the simulator I see this error:
[16:40:07] App: Unhandled Error: opendir failed for: "private/data"
listDir at app/index.js:14,8
readFiles at app/index.js:32,1
initialize at app/components/report.js:60,1
(this stack trace is wrong by the way. the "initialize" function doesnt exist in app/components/report.js. rather initialize calls a method in app/components/report.js)
11-18-2018 07:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

11-18-2018 07:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Perhaps you should use absolute path instead of relative path.
realtive path: private/data -> /private/data/private/data (not existing?)
absolute path: /private/data -> /private/data
12-06-2018 00:08 - edited 12-06-2018 00:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-06-2018 00:08 - edited 12-06-2018 00:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Indeed - I have the same error. The guide should be fixed to stop others like us falling into this trap. Similarly, dirIter is not created in the code snippet on the guide, so as soon as you do add the / to fix the line above you get a different error and have to fix yourself.
Expected (and works):
var listDir = listDirSync("/private/data");
var dirIter;
while((dirIter = listDir.next()) && !dirIter.done) {
console.log(dirIter.value);
}
Was originally using a custom folder path, but always getting the same error. Assumed it was because I hadn't written a file into the folder yet and so it legitimately wasn't there (really wish there were folderExists() and fileExists() functions added in with this update also!), but even when I swapped to the string in the guide I was still getting the same error.
[EDIT: I have now noticed openSync() on the API reference says user-created folders are not allowed anyway, so I now realise I was wasting my time waiting for listDirSync() in the new SDK update and have been misunderstanding its intended usage. This information should be on the guide also...]
Thanks for the simple solution above.
12-06-2018 07:10 - edited 12-06-2018 07:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-06-2018 07:10 - edited 12-06-2018 07:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Hi ObSkewer,
you can use the following function to check if a file exists:
function file_exists(filename) { let dirIter = ""; let listDir = listDirSync(""); while((dirIter = listDir.next()) && !dirIter.done) if(dirIter.value===filename) return true; return false; }
Usage:
if(file_exists("testfile.txt")) { console.log("testfile.txt exists!"); } else { console.log("testfile.txt not exists!"); }
This is only a workaround and not a very good solution because of many files in the directory but I think it is at this time the best solution.
If you often need to use this function it would be better to run the listDirSync()-loop one time at the beginnung und writing the result in a string and then only check the string instead of loop many times in the file-system.
12-06-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-06-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Awesome - thanks for that! Was going to look into writing the function myself, but had to head off to the day job straight after my last post, so you've just saved me a task this evening 😉

04-19-2019 14:17
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-19-2019 14:17
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I too was a bit stymied by the FileSystem documentation https://dev.fitbit.com/build/guides/file-system/ and https://dev.fitbit.com/build/reference/device-api/fs/ , and was also stuck where I needed to be checking for a presence of a file (I am trying to cache GPS watch data to overcome the GPS slowness).
Anyway, Nowo, your solution worked great—thank you so much.

