Error - ngcordova - $cordovaFile - Cannot call method 'then' of undefined


#1

Hello, I am using ngCordova - $cordovaFile to create a directory and to save some images.
I am getting error saying - Cannot call method ‘then’ of undefined… when trying to call $cordovaFile.createDir.

$cordovaFile.createDir("adExpress", true).then(function (result) {
        alert(result);
    }, function (err) {
        alert(err);
    });  

When I run above code I am getting error saying -TypeError: Cannot call method ‘then’ of undefined.

Can someone explain what is issue here?


#2

Have you injected the $cordovaFile module?


#3

Yes. Here is my controller

.controller('mediaController', function ($stateParams,$cordovaCamera, $cordovaFile,  ) {
 function getPicture(sourceType) {              
$cordovaFile.createDir("adExpress", false).then(function (result) {
            console.log(result);
        }, function (err) {
            console.log(result);
        });
    }
}

I am not understanding why I am seeing this error. While debug I am seeing proper $cordovaFile object.


#4

Any input on this please?


#5

Just curious, how do you have ng-codova/cordova loading? What order is your script tags?


#6

Here is How I am loading my ng-cordova/cordova.

 <script src="cordova.js"></script>
    <script src="lib/ng-cordova.js"></script>

FYI, I am able to successfully work with $cordovaCamera. I am facing problem with $cordovaFile.


#7

Can some one help me with this?

From where exactly I should download ng-cordova.js? I tried from this - https://www.npmjs.org/package/generator-ng-cordova


#8

Same problem. I have access to the object but the callback doesn’t seem to fire - this is for the upload method.


#9

ng-cordova should be before cordova.js, as per our documentation…

http://ngcordova.com/docs/

That npm package isn’t an official cordova, so you should be downloading the js file from the github page


#10

Apologies I miscommunicated, here is my script loading.

 <script src="lib/ng-cordova.js"></script>
<script src="cordova.js"></script>

Still I am having the same problem. Not sure How to fix this. still looking for any fix.


#11

I met the same problem. I modifying createDir method within ng-cordova.js. like this:

createDir: function (dir, replaceBOOL) {
    var q = $q.defer();
    
    getFilesystem().then(
        function (filesystem) {
            filesystem.root.getDirectory(dir, {
                create: true,
                exclusive: replaceBOOL
            },
            function () {
                q.resolve();
            },
            function () {
                q.reject();
            });
        }
    );
    
    return q.promise;

},

It’s Solved.

If you want to know more details.I suggest you look at this:AngularJS API $q
I hope this can help you.