0

Angular2:

I have a component and a service. Component calls service and it must return the response. The problem is that I don't know how to return the response. My component is like:

uploadFile(docId: any) {
        let pos = this.uploader.queue.length-1;
        //We pick the last one
        this._uplService.uploadDoc(this.uploader.queue[pos], docId, this.innerText)
    }

The service does this thing:

uploadDoc(file: FileItem, docId : any, title: String){
    let headers = this._httpWrapper.genetareAuthHeader("path", "POST");
    let user = this._userService.getUser();

    file.url = user.config.apiUrl+"path?options="+user.options;
    file.upload();
    file.onSuccess = function(response: string, status: number, headers: ParsedResponseHeaders) {
      console.log(response);
    }
    file.onError = function(response: string, status: number, headers: ParsedResponseHeaders) {
      console.error(response);
    }

My problem is to return response and error from the service to the component. I tried to do an observable from promise, from response etc. but observables are really hard and all the times I tried, 'uploadDoc' returned a function (file.onSuccess or file.onError) not the value returned by the function (response).

Shil Nevado
  • 716
  • 1
  • 11
  • 30
  • This question has been answered here: https://stackoverflow.com/a/43517846/5419266 – order Jul 26 '17 at 21:31
  • 1
    I didn't remember if I fixed that in some way, but I don't think that link helps. I'll explain. I had the response on 'service' (lines 'file.onError' and 'file.onSucces') but from there I have to pass the result to the controller (where there is the function 'uploadFile') but I don't know how to pass information from service to controller. – Shil Nevado Jul 27 '17 at 06:37

0 Answers0