1

Using Angular 2 with AOT compilation enabled (Angular universal), in a custom resolver I'm getting the error..

Uncaught (in promise): Error Error: Uncaught (in promise): Error

The problem seems to be isolated to when I return the result of a HttpClient call (which is just a simple number).

If I switch off server side rendering this all works correctly. Below is an extract of the code.

constructor(private httpClient : HttpClient)
{

}

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): number | Observable<number> | Promise<number> {

    try{
        return this.httpClient.get<number>(`/api/something/getnumber`);
    }catch(e){
         console.log(e);              
    }
    return -1;
}

Is this a fundamental issue in server side rendering?, or am I just missing some critical configuration that you need with server side rendering enabled?

2
  • 1
    This likely has something to do with the fact how the request is being performed on server side. try..catch is useless here because the function returns an observable of a result, not a result. Use .catch() to catch the error from an observable or something else. Commented Oct 24, 2017 at 13:48
  • When making a call server side, urls have to be absolute Commented Nov 17, 2017 at 15:39

2 Answers 2

1

It has to do with the fact that you are getting non-json data as a result. Add {responseType: 'text'} to your call to specify this.

return this.httpClient.get<number>(`/api/something/getnumber`, {responseType: 'text'});

See also the documentation Requesting non-JSON data

Not all APIs return JSON data. Suppose you want to read a text file on the server. You have to tell HttpClient that you expect a textual response:

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure the responseType syntax is correct here, but it does seem that switching it to return an object removed the error. I'm still a little confused why this wasn't giving me a better error even when I changed it to .catch() properly
@gmn - it depends on what you are returning. You will also get an error if you return null but using the above solution would fix that error as well. If changing your response to an object fixes the error then so would leaving the response as is and adding responseType:'text'. Which one you choose to do is up to you.
0

Try like this below code is my example :

@Injectable()
export class HomeActionResolve implements Resolve<any> {

    constructor(private http: Http) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        try {
            return this.http.get('assets/json/sample.json');
        } catch (e) {
            console.log(e);
        }
        return -1;
    }
}

export const HomeRoute: Routes = [
    {
        path: '',
        component: HomeComponent,
        resolve: {
            'pagingParams': HomeActionResolve
        }
    }
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.