[File Plugin] Cannot read property 'dataDirectory' of undefined

Forge Component
(4)
Published on 2018-10-16 by Pedro Oliveira
4 votes
Published on 2018-10-16 by Pedro Oliveira

Hi hi,

I am trying to read a directory on my mobile device, but I am not getting there. Whenever i call CreateDirectory or ListDirectory this error appears:

Cannot read property 'dataDirectory' of undefined

When debugging i get to here:


The problem is generated by the JS (I am using InternalStorage): 

var fileSystemType = $parameters.IsInternal ? cordova.file.dataDirectory : cordova.file.externalDataDirectory;

window.resolveLocalFileSystemURL(fileSystemType,
    function(fs) {
        $parameters.ErrorCode = "0";
        $parameters.ErrorMsg = "";
        $parameters.FileSystem = fs;

        $resolve();
    },
    function(error){
        $parameters.ErrorCode = error.code;
        $parameters.ErrorMsg = $actions.GetErrorMessage(error.code).Message;
       
        $resolve();
    }
);


The problem is in the "cordova.file.dataDirectory" part of the JS, how can I resolve this?

Help is appriciated,

Wilko

Hello Wilko,


That code won't work. The problem is both in the cordova.file.dataDirectory and in the cordova.file.externalDataDirectory parts of the JavaScript. Somehow, the JavaScript can't recognize the cordova object.


In order to resolve this, you just need to define the physical path of the directory that you want to read, for both Android and iOS.


I will leave this link for you. It is very useful to understand the File System Layouts and the physical paths on a real device (for both iOS and Android):
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/


As an example, let's say you want to list the content into the Documents folder in an Android device. You just need to define the path as:

$parameters.DirectoryPath = "Documents";


Let me know if you managed to do it.

Hope it helps,

Rui Barradas

Rui hi,

You mention a parameterDirectoryPath. Let's say I want to list the files in a location "Documents". The FilePlugin has an action ListDirectory which looks like:

The problem now is in the RequestDirectoryFromFile system action. The ListDirectory JS takes an Directory Object from GetDirectory, which takes a RootDirectory Object from RequestDirectoryFromFile, which gives the problem now. So I should replace RequestDirectoryFromFile and GetDirectory (or not use it) and change the JavaScript inside ListDirectory so the current Directory parameter there gets replaced by 

$parameters.DirectoryPath = "Documents";

I am lost here.

Hope you can give more more hints,

Wilko


Hello there Wilko,


Sorry if I wasn't clear enough.

You don't need to change the logic already implemented inside the actions provided by the Plugin! You just need to use these actions in your flows.


Let's say you want to list the content into the Documents folder in an Android device. You just need to call the ListDirectory action in your flow and define the physical path as an input parameter.

This parameter DirectoryPath that I mentioned is one JavaScript output parameter that you should use as the Path input parameter in the ListDirectory action (or you can define it directly when you call the ListDirectory action without defining it via JavaScript).


However, as you can see in the link above, iOS devices and Android devices have different File System Layouts.

So, you should take that into consideration and define the physical path according to the respective platform:


Hope it helps.

Let me know if you managed to do it.

Rui Barradas

Rui hi,

I am really sorry to bother you with this. I do as you told me, but ListDirectory calls RequestDirectoryFromFileSystem, which has the GetFileSystem javascript in it, which causes the error of cannot read property 'dataDirectory' of undefined, which cannot be solved you said. So somewhere I need to get rid of RequestDirectoryFromFileSystem, which is inside FilePlugin, and still manage to keep it work somehow.

Or should I copy the ListDirectory javascript and pass the directory as an object, then could you help me how to transform the text to the correct object?

Saludos,

Wilko

Solution

Hello Wilko,


It's okay, I'm just trying to help you :)

I am using this plugin to save and read files in my mobile app and it is working exactly as expected.

Again, you don't need to change anything inside the module FilePlugin.


Can you show me your code so I can check it? The flow when you're calling the FilePlugin ListDirectory action.


Thank you,

Rui Barradas

Solution

Rui hi,

The flow inside the GetDirectory is:

Form this:

The java script:

The ListDirectory call:

Debugging this with my phone connected shows the error inside GetFileSystem javascript with the cordova.file.dataDirectory being undefined, which is called from ListDirectory.

Saludos,

Wilko

Rui hi,

I did something else, I installed FileSample from the Forge. It uses the same FilePlugin module. From my demo app the GetFileSystem javascript in RequestDirectoryFromFileSystem called from ListDirectory gives an error (one step further down the road in the javascript):

The same the GetFileSystem javascript in RequestDirectoryFromFileSystem called from ListDirectory called from the FileSample doesn't give an error:

Would it be something in the build of the app of DemoApp versus the build of the app of FileSample that triggers the error in the GetFileSystem javascript?

Saludos,

Wilko

Hi again Wilko,

I was off during some time, but I'm back now.

Did you manage to solve the problem?

If you want, I can check it and run some tests in my side.

Rui hi,

After some more digging regarding the error "Cannot read property 'dataDirectory' of undefined", i assume the underlying problem has to do with the assembly of the build of the app. probably there is logic that determines what libraries to include when doing the build, and if in the dependency tree for the build there is a reference to a module that somewhere is missing or has broken links, it doesn't get included in the build, but runtime there is a link missing or broken link (in this case 'dataDirectory') it tries to find it but it is not there. So i checked all the dependencies and fixed what was broken or missing and then the error doesn't happen. My demo app had some broken or missing dependencies, and the FileSample didn't have them.

It took some time figure this out, but yes, it is working.

I saw some other posts with variations on not being able to read properties, i assume they have the same cause.

Thanks for thinking along.

Wilko

Hi Wilko,

I'm glad you managed to figure it out.


Cheers,

Rui Barradas