I have an issue with a function that returns an Observable. The problem is that when the function is called, the parameter works fine, but its value becomes undefined
within the Observable.
This is the function in question:
import {Observable} from 'rxjs/Observable';
export function ToBase64(inputFile) {
console.log('Input:', inputFile); //The variable has the correct value here
let observable = new Observable(observer => {
try {
console.log('Input in observable:', inputFile); //here it is undefined :(
var image = {};
var reader = new FileReader();
reader.onload = (e) => {
image.base64 = btoa(e.target.result);
var stringBase64 = `data:${image.filetype};base64,${image.base64}`;
observer.next(stringBase64);
observer.complete()
};
console.log(inputFile);
var inputFile = inputFile.files[0];
image.filetype = inputFile.type;
image.size = inputFile.size;
image.filename = inputFile.name;
reader.readAsBinaryString(inputFile);
} catch (e) {
observer.error(e);
}
});
return observable;
}
This function is being used in an Angular2 App like this:
//imports...
export class MedicosListComponent {
@ViewChild('imagenMedico') imagenMedico:any;
private image:string;
constructor() {
}
showAllWeas() {
ToBase64(this.imagenMedico.nativeElement).subscribe(
data => {
alert(data)
},
error => alert(error)
)
}
}
The medicoList
variable is of type file input.
Thank you in advance for any assistance.