react-native-blob-util: Cannot read property 'rnfbEncode' of undefined, js engine: hermes

My code

import {Platform} from 'react-native'
import ReactNativeBlobUtil, {ReactNativeBlobUtilConfig} from 'react-native-blob-util'


export const options = (name: string, extension = 'pdf', notification = true) => {
  const {fs} = ReactNativeBlobUtil
  const {
    dirs: {DocumentDir, DownloadDir},
  } = fs
  return Platform.select({
    ios: {
      fileCache: true,
      path: `${DocumentDir}/${name}`,
      title: name,
    },
    android: {
      fileCache: true,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification,
        title: name,
        path: `${DownloadDir}/${name}.${extension}`,
      },
    },
  }) as ReactNativeBlobUtilConfig
}

import {useCallback, useState} from 'react'
import {Platform} from 'react-native'
import ReactNativeBlobUtil from 'react-native-blob-util'

import {options} from '@/utils/blob'
import {filesPermission} from '@/utils/hardwarePermissions'

export const useFetchBlob = () => {
  const [progress, setProgress] = useState<number>()

  const fetchBlob = useCallback(
    async (
      url: string,
      name: string,
      extension?: string,
      headers?: {
        [key: string]: string
      },
    ) => {
      try {
        const hasDownloadPermission = await filesPermission()
        if (hasDownloadPermission) {
          const {config} = ReactNativeBlobUtil

          const res = await config(options(name, extension))
            .fetch('GET', url, headers)
            .progress((received, total) => {
              setProgress(received / total)
            })

          if (Platform.OS === 'ios') {
            ReactNativeBlobUtil.ios.previewDocument(res.path())
          }
        } else {
        }
      } catch (error) {} finally {
        setProgress(undefined)
      }
    },
    [progress],
  )

  return {
    progress,
    fetchBlob,
  }
}


“react-native”: “0.72.7” “react-native-blob-util”: “^0.19.6”,

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 20 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @mMarcos208 I think the issue is with package version, I also stumbled on the same error. So, I downgraded the version to react-native-blob-util@0.19.4 it works for me. Hope it helps.

Comment the two line(273, 274) in fetch.js

                         fs.session(options.session).add(data);
                 }
                 respInfo.rnfbEncode = rawType;
-                if ('uninit' in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
-                    respInfo = responseInfo;
+                // if ('uninit' in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
+                //     respInfo = responseInfo;
                 resolve(new FetchBlobResponse(taskId, respInfo, data));
             }

Hi @mMarcos208, I have fixed this issue by wrapping the path with path: ReactNativeBlobUtil.wrap(filePath)

` import {Platform} from ‘react-native’ import ReactNativeBlobUtil, {ReactNativeBlobUtilConfig} from ‘react-native-blob-util’

export const options = (name: string, extension = ‘pdf’, notification = true) => { const {fs} = ReactNativeBlobUtil const { dirs: {DocumentDir, DownloadDir}, } = fs return Platform.select({ ios: { fileCache: true, path: ReactNativeBlobUtil.wrap(${DocumentDir}/${name}), title: name, }, android: { fileCache: true, addAndroidDownloads: { useDownloadManager: true, notification, title: name, path: ReactNativeBlobUtil.wrap( ${DownloadDir}/${name}.${extension}), }, }, }) as ReactNativeBlobUtilConfig } `

diff --git a/node_modules/react-native-blob-util/fetch.js b/node_modules/react-native-blob-util/fetch.js
index ed4e67f..fe541e5 100644
--- a/node_modules/react-native-blob-util/fetch.js
+++ b/node_modules/react-native-blob-util/fetch.js
@@ -270,8 +270,8 @@ export function fetch(...args: any): Promise {
                         fs.session(options.session).add(data);
                 }
                 respInfo.rnfbEncode = rawType;
-                if ('uninit' in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
-                    respInfo = responseInfo;
+                // if ('uninit' in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
+                //     respInfo = responseInfo;
                 resolve(new FetchBlobResponse(taskId, respInfo, data));
             }
 

It work for me.