React-native-fs: Cannot read property 'startsWith' of undefined

Created on 27 Sep 2017  路  10Comments  路  Source: itinance/react-native-fs

I installed the library and linked it with react-native link react-native-fs.
I try to run the minimal example:

RNFS.readDir(RNFS.MainBundlePath) // On Android, use "RNFS.DocumentDirectoryPath" (MainBundlePath is not defined)
    .then((result) => {
      console.log('GOT RESULT', result);

      // stat the first file
      return Promise.all([RNFS.stat(result[0].path), result[0].path]);
    })
    .then((statResult) => {
      if (statResult[0].isFile()) {
        // if we have a file, read it
        return RNFS.readFile(statResult[1], 'utf8');
      }

      return 'no file';
    })
    .then((contents) => {
      // log the file contents
      console.log(contents);
    })
    .catch((err) => {
      console.log(err.message, err.code);
    });

which gives me (on Android API 26)

Cannot read property 'startsWith' of undefined

I tried with a lower API (24) which gives me

undefined is not an object (evaluation 'path.startsWith')

My MainActivity.java file looks different than the one suggested when you manually link the library

package com.MYAPP;
import com.facebook.react.ReactActivity;
public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "MYAPP";
    }
}

But when I change it to this

package com.MYAPP;
import com.facebook.react.ReactActivity;
public class MainActivity extends ReactActivity implements DefaultHardwareBackBtnHandler{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
      .setApplication(getApplication())
      .setBundleAssetName("index.android.bundle")
      .setJSMainModuleName("index.android")
      .addPackage(new MainReactPackage())
      .addPackage(new RNFSPackage())      // <------- add package
      .setUseDeveloperSupport(BuildConfig.DEBUG)
      .setInitialLifecycleState(LifecycleState.RESUMED)
      .build();

    mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);

    setContentView(mReactRootView);
    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "MAYAPP";
    }
}

The app does not even compile...

react-native-cli: 2.0.1
react-native: 0.48.3
npm: 5.3.0

Any ideas?

Most helpful comment

Finally found what was wrong, I used RNFS.MainBundlePath on android instead of what was suggested RNFS.DocumentDirectoryPath 馃檲馃檲

All 10 comments

What is the console-output in line 3? Pls post this as a comment.

Internally, all methods like "stat" and "readDir" as an example pass through the filename-parameter to normalizeFilePath. If the given "path" is undefined or a promise or so such an exception will be thrown as you're reporting.

So i guess that something is odd with the results-parameter.

@itinance output at line 3? You mean in the error?

According to the code, that you've postet:

RNFS.readDir(RNFS.MainBundlePath) // On Android, use "RNFS.DocumentDirectoryPath" (MainBundlePath is not defined)
    .then((result) => {
      console.log('GOT RESULT', result);

is the line number 3 this one:

console.log('GOT RESULT', result);

What is the output?

Sorry for the late reply. Ive been away for quite a while. Unfortunately the project progressed and I cannot reproduce the error anymore :-/

thanks for reaching back. will close due to unreproducability

I was recently getting this same error while running an app on an Android simulator. Other apps developed using the same base code worked flawlessly. Restarts and cleans did nothing to help.

The error went away when I deleted from the simulator all of the other apps I had built.

I am also encountering this error and still not able to resolve it. Here are my build config

    compileSdkVersion 23
    buildToolsVersion 25.0.0
    defaultConfig {
        applicationId "com.gorailrn"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }

Finally found what was wrong, I used RNFS.MainBundlePath on android instead of what was suggested RNFS.DocumentDirectoryPath 馃檲馃檲

Cannot read property 'startsWith' of undefined

I got that error and resolved by putting 
if(filepath != undefined){
 fs.readFile(filepath, function(err, filedata) {

})
}

as path is undefined until it choose the image and get path.
if it helps :)
thanks

Finally found what was wrong, I used RNFS.MainBundlePath on android instead of what was suggested RNFS.DocumentDirectoryPath 馃檲馃檲

this one work for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CesarLanderos picture CesarLanderos  路  4Comments

MuppaAkhil picture MuppaAkhil  路  3Comments

duckmyanmar picture duckmyanmar  路  3Comments

AbdelkaderOumrani picture AbdelkaderOumrani  路  3Comments

onzag picture onzag  路  3Comments