Search code examples
javascriptserviceangular7

API not getting called in service file Angular 7


I am working with a news App. When I try to call API, it is not getting called in service file function. When I try to console it is getting consoled but when I see network tab there is no API called.

Component file:

newPostView(postId: { split: (arg0: string) => any[]; }) {
       postId = postId.split("-")[1];
       this.data = {
           postId: postId,
           postType: localStorage.language
       }
       this._newsService.newsCount(this.data);
}

Below is my function in the service file.

newsCount(data) {
        console.log("post data", data);
        return this.http.put(config.baseApiUrl + 'post-views', data);
}

Solution

  • If you are returning from the service file then you should subscribe in component file. So your updated code should look like this.

    Component File:

    newPostView(postId: { split: (arg0: string) => any[]; }) {
            postId = postId.split("-")[1];
            this.data = {
                postId: postId,
                postType: localStorage.language
            }
            this._newsService.newsCount(this.data).subscribe((res:any) =>{
    
            });
    }