2

I have seen this error on SO quite a few times, all I can find on it is that I need to make sure that I have my service Provided in my app.modules, and then call it in my constructor of my component. I have done this and am still getting the error. Also I have both http and HTTPMODULES in my application. The error only occurs when I use the delete functionality in my application. Here is the error error_handler.js:45 EXCEPTION: Cannot read property 'get' of undefined, here is some relevant code....

app.module.ts

  import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';

    import { HttpModule, JsonpModule }     from '@angular/http'; <------------HTTP

    import { AppComponent }  from './app.component';
    import { PostRantComponent } from './postRant.component';
    import { PostDataService } from './PostData.Service';    <------------service
    import { Constants } from './app.const.service';



    import { Routing } from './app.routes';
    import { FormsModule }   from '@angular/forms';


    @NgModule({
        imports: [NgbModule.forRoot(), BrowserModule, Routing, FormsModule,   HttpModule, JsonpModule],
        declarations: [AppComponent,,PostRantComponent],
        providers: [PostDataService, Constants],
        bootstrap: [AppComponent]
     })
     export class AppModule { }

Service (tried cutting it down to just show relevant code)

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Observable';
import { PostViewModel } from './models/Post';
import { Constants } from './app.const.service';

@Injectable()
 export class PostDataService{

private actionUrl: string;
private headers: Headers;
constructor( private _http: Http, private _constants: Constants ){

    this.actionUrl = _constants.ServerWithApi;

    this.headers = new Headers();
    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Accept','application/json');
}

public GetAll = (): Observable<PostViewModel[]> => {
    return this._http.get(this.actionUrl)
    .map((response: Response) => <PostViewModel[]>response.json())
    .catch(this.handleError);
}

public Delete = (id: string) =>{
   return this._http.delete(this.actionUrl + id)
      .map(res => res.json())
      .catch(this.handleError);
}    

}

Component

import { Component, Attribute, OnInit,ViewChild } from '@angular/core';
import { PostViewModel } from './models/Post';
import { PostDataService } from './PostData.Service';
import { Constants } from './app.const.service';

@Component({
   selector: 'postRant',
   templateUrl: 'html/postRant.html',
})
export class PostRantComponent implements OnInit {
   txtTitle: string;
    txtDescription: string;

    public myPosts : Array<PostViewModel>;
    public newPost : PostViewModel = new PostViewModel();
    constructor(private auth:Auth, private _dataservice: PostDataService){ 

  }

ngOnInit(){
  this.getAllItems();
}

private getAllItems():void {
  this._dataservice
    .GetAll()
    .subscribe((Post: Array<PostViewModel>) => this.myPosts = Post,
      error => console.log(error),
        () => console.log('get all items complete'))
}

delete(id){
  console.log(id);
   this._dataservice.Delete(id)
    .subscribe((res) => {
      this.myPosts = res;
    });
      var index = this.myPosts.findIndex(x => x.id == id);
      this.myPosts.splice(index, 1);
  }
}

If you are interested in all the code I have it posted on my git located here, however it is rather large.

EDIT picture of error....enter image description here

1 Answer 1

1

it appears that the error is produced by line 52 of PostData.Service.ts i.e. var applicationError = error.headers.get('Application-Error');

this makes me guess that your GetAll Http call is erroring out, but the server you are asking for data is not returning data in the format of error.headers

Add a debugger; to the handleError and check the object that it is receiving.

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

1 Comment

You are correct the problem was not on the front end of my application, it is on the backend in my api. Thank you!

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.