TypeError: error.json is not a function

前端 未结 5 1634
陌清茗
陌清茗 2021-01-04 20:37

Edit: Read the part at the end of the question!

I get this error:

My service code:

import { Http, Response, Headers } from \         


        
5条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 21:34

    Don't catch and rethrow. Just handle the exception when you consume the service.

     .map(response => response.json());
    

    Or if you want to handle the exception in your service, and just return an error you can do the following

     .map(response => response.json())
                .catch(error => Observable.throw("Error in x service"));
    

    You can see the documentation of Observable.throw and how it is used.

    The error object you are getting is malformed. You can add some console logs to see what you are getting back. But it is causing issues.

提交回复
热议问题