Im attempting to get a b64 value for an image inside my controller. For some reason only in this case when I set a variable inside the filereader.onload it will clear itself. Inside the function I am able to log it out or send it to another function, however once the function chain has "ended" the variable is no longer defined.
Ive tried multiple ways of doing this and I cant seem to get it to work. Heres my code snippet;
function createProfilec(user) {
var regionasstring = region.toString();
var ctypetostring = ctype.toString();
imageset();
console.log($scope.imageIdForDB);
var ref = Ref.child('users/' + user.uid), def = $q.defer();
ref.set({type: 'company',companyname: Cname, region: regionasstring, contactnumber: number, companytype: ctypetostring, date: Firebase.ServerValue.TIMESTAMP, image: $scope.imageIdForDB}, function(err) {
$timeout(function() {
if( err ) {
def.reject(err);
}
else {
def.resolve(ref);
}
});
});
return def.promise;
}
function imageset(){
var filesSelected = document.getElementById('inputFileToLoad').files;
if (filesSelected.length > 0)
{
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
imageid = fileLoadedEvent.target.result;
setimageb64(fileLoadedEvent.target.result);
};
fileReader.readAsDataURL(fileToLoad);
}
}
function setimageb64(ID){
console.log(ID);
$scope.imageIdForDB = ID;
}
Cheers