itinance / itinance/react-native-fs

RNFS.exists() returns True but RNFS.readFile fails with error: File doesn't exist

Open
#497 15 comments 18 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
5k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

```
async _loadCustomData(fileName, fieldName) {
const filePath = RNFS.ExternalStorageDirectoryPath + '/Download/' + fileName;

if (await RNFS.exists(filePath)) {
console.log("custom data file for " + fieldName + " exists");
let customData = RNFS.readFile(filePath);
customData = JSON.parse(customData);

let newState = {};
newState[fieldName] = customData;
this.setState(newState);
} else {
console.log("No such file as ", fileName);
}
}
```

The if-condition evaluates to True and console.log displays that the file exists, but, I am unable to read the contents of the file.
customeData evaluates to an object like this {_45: 0, _65: 0, _55: null, _75: null}

So, to read this Object, I did this:
```
RNFS.readFile(filePath)
.then((contents) => {
console.log(contents);
customData = JSON.parse(contents);

let newState = {};
newState[fieldName] = customData;
this.setState(newState);
})
.catch((err) => {
console.log(err.message, err.code);
});
```

Now, I get the error: "ENOENT: no such file or directory, open '/storage/emulated/0/Download/chimp-list-additional.json' ENOENT"

I have tried the approach here https://github.com/itinance/react-native-fs/issues/471 but it doesn't work for me.

Please help!!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.