Jacob Notte Jacob Notte - 25 days ago 8
Javascript Question

Angular 2 Webworkers Http Uncaught (in promise): not implemented

I've been stuck on an error that I'm not completely sure how to solve.
My application is made in Angular2 and runs completely in a webworker largely based on this tutorial http://www.syntaxsuccess.com/viewarticle/web-workers-in-angular-2.0

My first feature was an implementation of socket.io which is working perfectly(also with observables etc..) but now I want to use the Http service of Angular2 and I get the following error:

enter image description here

My code of the service is like this and the error arrises when I call validateAccessToken (I have to add the .js on my imports otherwise I get a 404 on the files within the webworker):

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from "@angular/http";
import { environment } from "../../../environments/environment.js";
import { Observable } from "rxjs/Observable.js";
import 'rxjs/add/operator/toPromise.js';
import 'rxjs/add/operator/map.js';

@Injectable()
export class AuthService {
headers: Headers;
options: RequestOptions;
url: string;

constructor(private http:Http) {
this.url = environment.authServerUrl;
}

validateAccessToken(token) {
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
};

extractData(response: Response) {...}
handleError(error: any) {...}
}


I know the question is quite vague but with the information I get from the error it's not really clear what's going wrong for me.

Answer

The CookieXSRFStrategy is default enabled by Angular2 and used by http. The webworker does not have DOM access to get the cookie to insert in the http headers. And thus throws the error Uncaught not implemented.

You should implement your own CookieXSRFStrategy strategy which at least does not throw this error ;)