I have a function set up in my AngularJS service:
var readFile = function (path, file) {
console.info("Attempting to read the file with URI: " +path+ " "+file);
$cordovaFile.checkFile(path, file).then(function (success) {
console.log(success);
var deferred = $q.defer();
$cordovaFile.readAsText(path, file)
.then(function (success) {
// success
console.log("Success");
console.log(success);
deferred.resolve(success);
}, function (error) {
console.error(error);
deferred.reject(error);
});
return deferred.promise;
}, function (error) {
console.log(error);
});
}
In the success callback, there is a function defined for reading the file.
However, I keep encountering an exception message at this location:
TypeError: Cannot read property 'then' of undefined
But when I modify the code like this:
$cordovaFile.checkFile(path, file).then(function (success) {
console.log(success);
}, function (error) {
console.log(error);
});
it works without any issues.
I want to only execute the file reading if the file exists, but I'm unsure of the correct approach to take.
The service method is called like so:
DeviceSrv.readFile(cordova.file.externalRootDirectory+"/"+APP_CONST.MP3_FOLDER_NAME+"/sounds/", APP_CONST.MP3_FILE_NAME)
.then(function(value){
console.log(value);
var parsedObject = JSON.parse(value);
console.log(parsedObject);
$scope.availableSounds = parsedObject.availableSounds;
$scope.availablePrepreparedSounds = parsedObject.availablePrepreparedSounds;
});
How should I update my code to ensure it runs correctly?
Thank you for any advice provided.