Need help getting cordova file plugin right


#1

Hi there,

I want to get a backup service running that saves some data to file using the cordova file library. In the checkBackupExists function, I managed to move from error code 5 - ENCODING_ERR to return code 1 - NOT_FOUND_ERR which makes sense for the file is not existing.

I removed the file:// prefix to get error code 1.

For the save function I keep getting error code 3 - ABORT_ERR , which I do not understand why it happens? I am running my stuff in iOS emulator right now.

Finally I would have to get the load function going, but need to fix the save function first…

    this.checkBackupExists = function(){
		var deferred = $q.defer();

		$cordovaFile.checkFile(cordova.file.documentsDirectory.replace('file://',''), backupFileName )
		.then(function (success) {
			deferred.resolve(true);
		}, function (error) {
			// error
			if(error.code==1)
				deferred.resolve(false);
			else
				deferred.reject(error);
		});
		return deferred.promise;
	}
	
	this.saveBackupFile = function(data){
		
		var currentDate = new Date();
		
		var data = {};
		data['date'] = currentDate.toISOString();
		data['data'] = data;
		
		var dataJSON = JSON.stringify(data);
		
		var deferred = $q.defer();

		$cordovaFile.writeFile(cordova.file.documentsDirectory.replace('file://',''), backupFileName , dataJSON, true)
		.then(function (success) {
			// success
			deferred.resolve(success);
		}, function (error) {
			// error
			console.log('Error writing file');
			deferred.reject(error);
		});

		return deferred.promise;
	};

    this.loadData = function(){
		var deferred = $q.defer();
		
		$cordovaFile.readAsText(cordova.file.documentsDirectory.replace('file://',''), backupFileName)
		.then(function (success) {
			deferred.resolve(success);
		}, function (error) {
			// error
			console.log('Error reading file');
			deferred.reject(error);
		});
		return deferred.promise;
	}