react-native-fs: RNFS.exists() returns True but RNFS.readFile fails with error: File doesn't exist
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!!
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 18
- Comments: 15
same issue on copyFile
exists returns true , but copy prompt ‘The file “0C635EA5-5BA8-4933-8C01-E570FB7EDACA.jpg” doesn’t exist.’
Not sure how this relates to those seeing issues with
unlink
orreadFile
, but for anyone else running into this issue specifically whencopyFile
returnsThe file “<sourceFilename>” doesn’t exist.'
, for us it turned out that the destination directory sometimes didn’t exist. We resolved the issue by just checking for/creating the destination dir. So something like…copyFile
could probs benefit from a more specific error message sinceThe file “<sourceFilename>” doesn’t exist.'
is a bit misleading, but we’re running version2.16.6
, so not sure if there’s been a fix for pushed since then.Hopefully this helps someone!
Hi,
Getting
true
onif (await RNFS.exists(filePath))
doesn’t mean that the file exists. Since it returns aPromise<boolean>
, the if condition will always be true.Try the following
RNFS.exists(filePath).then((status)=>{ if(status){ console.log('Yay! File exists') } else { console.log('File not exists') } })
same here.