RNFS.exists() 总是 returns 真

RNFS.exists() always returns TRUE

我正在使用 react-native-fs,出于某种原因,每当我使用 exists() 方法时,它总是返回 TRUE。我的代码示例如下所示:

let path_name = RNFS.DocumentDirectoryPath + "/userdata/settings.json";

if (RNFS.exists(path_name)){
    console.log("FILE EXISTS")
    file = await RNFS.readFile(path_name)
    console.log(file)
    console.log("DONE")
}
else {
    console.log("FILE DOES NOT EXIST")
}

控制台的输出是 "FILE EXISTS",然后抛出一个错误:

Error: ENOENT: no such file or directory, open /data/data/com.test7/files/userdata/settings.json'

exists的方法怎么能存在,而不用readFile的方法呢?

进一步检查,似乎 RNFS.exists() 总是返回 true,无论文件名是什么。为什么它总是返回 true?

显示 path_name 表示 /data/data/com.test7/files/userdata/settings.json

即使我将我的代码更改为如下代码这样荒谬的东西:

if (RNFS.exists("blah")){
    console.log("BLAH EXISTS");
} else {
    console.log("BLAH DOES NOT EXIST");
}

它仍然评估为 true 并显示消息:

BLAH EXISTS

我已经显示了目录的内容并确认这些文件不存在。

那是因为RNFS.exists()returns一个Promise。将一个 Promise 对象放在 if statement 的测试中将永远为真。

改为这样做:

if (await RNFS.exists("blah")){
    console.log("BLAH EXISTS");
} else {
    console.log("BLAH DOES NOT EXIST");
}

或:

RNFS.exists("blah")
    .then( (exists) => {
        if (exists) {
            console.log("BLAH EXISTS");
        } else {
            console.log("BLAH DOES NOT EXIST");
        }
    });