如何在Expo中使用FileSystem.getInfoAsync()?

问题描述 投票:0回答:1

我正在尝试测试文件是否存在expo。

这是expo doc:返回如果此URI不存在任何项,则返回{exists:false,isDirectory:false}。否则返回一个对象

if (this._liste.length === 0) {
               let tmp =FileSystem.getInfoAsync('file://exemple.json');

                if(tmp.exists === false){
                    alert("not found")
                }
            }

文件不存在,我确定并且警报没有显示。当我打印tmp时,我有一个Object但是当我打印tmp.exist时我有一个未定义的。

react-native filesystems expo
1个回答
1
投票

getInfoAsync()返回此(示例):

Object {
 "exists": 1,
 "isDirectory": true, // notice isDirectory == true
 "modificationTime": 1532926143,
 "size": 102,
 "uri": "file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076-b4b71df1e55c/avatar/",
}

如果要读取目录中的文件,请使用FileSystem.readDirectoryAsync


0
投票

getInfoAsync是一个异步函数,因此您不能像普通函数一样使用它。它返回一个Promise,因此你需要使用await或者然后:

等待

let tmp = await FileSystem.getInfoAsync('file://exemple.json');
// use tmp.exists

然后

FileSystem.getInfoAsync('file://exemple.json').then(tmp => {
// use tmp.exists
});
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.