I'm trying to access a JSON file in the same folder as my App.js file with expo-file-system to read and write from and to it, but I can't seem to be able to no matter what I do.
Here's an image of my project's folder (yes weird file names but that's not important right now)
Expo version 51.0.38
I tried using FileSystem.bundleDirectory with my file name:
import * as FileSystem from "expo-file-system"
...
...
...
const JSONPath = FileSystem.bundleDirectory + "myData.json";
console.log(JSONPath); // outputs asset:/myData.json
FileSystem.readAsStringAsync(JSONPath).then(res => {
console.log(JSON.stringify(res)); // Doesn't write anything to the console
});
but this throws an exception:
Console Warning: Possible unhandled promise rejection (id: 2): Error: Call to function 'ExponentFileSystem.readAsStringAsync' has been rejected. -> Caused by: java.io.FileNotFoundException: myData.json
I also tried the same but with FileSystem.documentDirectory:
import * as FileSystem from "expo-file-system"
...
...
...
const JSONPath = FileSystem.documentDirectory + "myData.json";
console.log(JSONPath); // outputs file:///data/user/0/host.exp.exponent/files/myData.json
FileSystem.readAsStringAsync(JSONPath).then(res => {
console.log(JSON.stringify(res)); // Doesn't write anything to the console
});
but this throws another exception:
Console Warning: Possible unhandled promise rejection (id: 3): Error: Call to function 'ExponentFileSystem.readAsStringAsync' has been rejected. -> Caused by: java.io.FileNotFoundException: /data/user/0/host.exp.exponent/files/myData.json (No such file or directory)
I also tried using @expo/json-file, but it threw an error saying that it uses fs, which is incompatible with Expo.
So I'm stuck here, I've been searching for the whole day and I found nothing, so any help would be very much appreciated. Also, while I'm at it, what's the difference between documentDirectory and bundleDirectory ??
Update: I ended up using expo-sqlite
instead of the JSON files, it works better in my case because it acts as a local database and can be queried on.
Here's the official documentation -> https://docs.expo.dev/versions/latest/sdk/sqlite/