Expo File System - Please make sure directory '(null)' exists before calling downloadAsync.

Please provide the following:

  1. SDK Version: react-native-unimodules: 0.12.0
  2. Platforms(Android/iOS/web/all): iOS

I am following the example on the expo documentation.

const downloadFilePath = `${FileSystem.documentDirectory}mypath/`;


export const fetchAndSaveOneFile = ({
  url,
  filename,
  extension,
}: {
  url: string;
  filename: string;
  extension: string;
}): Promise<string> => {
  return new Promise((resolve, reject) => {
    checkDirectory().then(async () => {
      try {
        const result = await FileSystem.downloadAsync(url, `${downloadFilePath}${filename}.${extension}`);
        console.log('Result: ', result);
        if (result?.uri) {
          resolve(result?.uri);
        }
      } catch (error) {
        console.error(`ERROR: Expo File System ${url}`, error);
        reject(error);
      }
    });
  });
};

const checkDirectory = async (): Promise<void> => {
  if (downloadFilePath) {
    try {
      const dirInfo = await FileSystem.getInfoAsync(downloadFilePath);
      console.log('Dir Info: ', dirInfo);
      if (!dirInfo.exists) {
        await FileSystem.makeDirectoryAsync(downloadFilePath, {intermediates: true});
      }
    } catch (error) {
      console.error(`ERROR: Expo Check Directory: ${downloadFilePath}`, error);
    }
  }
};

I already checked the folder and exist. So I am not sure what is the root cause of the problem.

I also iterate over a more structure way like this:

export const fetchAndSaveOneFile = async ({
  url,
  filename,
  extension,
}: {
  url: string;
  filename: string;
  extension: string;
}): Promise<any> => {
  try {
    await checkDirectory();
    const result = await FileSystem.downloadAsync(url, `${downloadFilePath}${filename}.${extension}`);
    if (result?.uri) {
      return result?.uri;
    }
  } catch (error) {
    console.error(`ERROR: Expo File System ${url}`, error);
    throw new Error(`ERROR: Expo File System ${url} ${error.message}`);
  }
};

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.