1

I have a single form which is used to add a new event or edit an existing event. I check the URL parameter and if an id is present then I would like to subscribe to an editEvent method from my EventService, otherwise subscribe to addEvent method from EventService and add the new event. Adding an event works fine whereas if an ID is passed in the URL then i get an error like Cannot read property 'subscribe' of undefined. I am returning an observable in my editEvent method (same as in addEvent) and it is not nested inside another call. Below is my code.

<form class="form-horizontal" [formGroup]="addEventForm" (ngSubmit)="addEvent()">

event-entry.component.ts

addEvent() {
        console.log(this.addEventForm.value);
        if(this.editedEventId){
            console.log('editing event');
            //If the editedEventId is passed via url then we are Editing an event
          this.eventService.editEvent(this.editedEventId,this.addEventForm.value).subscribe(
                data => console.log(data),
                error => console.log(error)
            );
        }else{
            console.log('adding event');
            //else we are creating an event
             this.eventService.addEvent(this.addEventForm.value).subscribe(
                data => console.log(data),
                error => console.log(error)
            );
        }

        //to reset the form after submission
        this.addEventForm.reset();
    }

event.service.ts

addEvent(event:Events) {
        this.events.push(event);
        const body = JSON.stringify(event);
        const headers = new Headers({
            'Content-Type': 'application/json'
        });
        return this.http.post('http://localhost:3000/event', body, {
            headers: headers
        })
            .map((response:Response) => response.json())
            .catch((error:Response) => Observable.throw(error.json()));
    }

editEvent(eventId:number, event:Events){
        console.log('Entering editEvent in event service');
        let url = 'http://localhost:3000/event/'+eventId;
        const body = JSON.stringify(event);
        const headers = new Headers({
            'Content-Type': 'application/json'
        });
        return this.http.put(url, body, {
            headers: headers
        })
            .map((response:Response) => response.json())
            .catch((error:Response) => Observable.throw(error.json()));

    }

I am new to angular 2. I tried all solutions available online (like using switchMap) but none worked. The full stack trace of the error.

ERROR TypeError: Cannot read property 'subscribe' of undefined
    at EventEntryComponent.addEvent (eval at <anonymous> (bundle.js:901), <anonymous>:120:86)
    at Object.eval [as handleEvent] (EventEntryComponent.html:3)
    at handleEvent (eval at <anonymous> (bundle.js:132), <anonymous>:12120:138)
    at callWithDebugContext (eval at <anonymous> (bundle.js:132), <anonymous>:13412:42)
    at Object.debugHandleEvent [as handleEvent] (eval at <anonymous> (bundle.js:132), <anonymous>:13000:12)
    at dispatchEvent (eval at <anonymous> (bundle.js:132), <anonymous>:9020:21)
    at eval (eval at <anonymous> (bundle.js:132), <anonymous>:10948:20)
    at SafeSubscriber.schedulerFn [as _next] (eval at <anonymous> (bundle.js:132), <anonymous>:4069:36)
    at SafeSubscriber.__tryOrUnsub (eval at <anonymous> (bundle.js:87), <anonymous>:238:16)
    at SafeSubscriber.next (eval at <anonymous> (bundle.js:87), <anonymous>:185:22)
    at Subscriber._next (eval at <anonymous> (bundle.js:87), <anonymous>:125:26)
    at Subscriber.next (eval at <anonymous> (bundle.js:87), <anonymous>:89:18)
    at EventEmitter.Subject.next (eval at <anonymous> (bundle.js:151), <anonymous>:55:25)
    at EventEmitter.emit (eval at <anonymous> (bundle.js:132), <anonymous>:4043:76)
    at FormGroupDirective.onSubmit (eval at <anonymous> (bundle.js:434), <anonymous>:4859:23)
    at Object.eval [as handleEvent] (EventEntryComponent.html:3)
    at handleEvent (eval at <anonymous> (bundle.js:132), <anonymous>:12120:138)
    at callWithDebugContext (eval at <anonymous> (bundle.js:132), <anonymous>:13412:42)
    at Object.debugHandleEvent [as handleEvent] (eval at <anonymous> (bundle.js:132), <anonymous>:13000:12)
    at dispatchEvent (eval at <anonymous> (bundle.js:132), <anonymous>:9020:21)
    at eval (eval at <anonymous> (bundle.js:132), <anonymous>:9612:20)
    at HTMLFormElement.eval (eval at <anonymous> (bundle.js:441), <anonymous>:2735:53)
    at ZoneDelegate.invokeTask (eval at <anonymous> (bundle.js:4472), <anonymous>:424:31)
    at Object.onInvokeTask (eval at <anonymous> (bundle.js:132), <anonymous>:4346:37)
    at ZoneDelegate.invokeTask (eval at <anonymous> (bundle.js:4472), <anonymous>:423:36)
    at Zone.runTask (eval at <anonymous> (bundle.js:4472), <anonymous>:191:47)
    at HTMLFormElement.ZoneTask.invoke (eval at <anonymous> (bundle.js:4472), <anonymous>:486:38)

Any help would be appreciated.

3
  • Post the complete stack trace of the error. Also, you don't need to stringify the body and to set the content-type header. Angular does that for you. Commented Jul 2, 2017 at 14:48
  • Thanks. added the full stack trace of the error. Commented Jul 2, 2017 at 15:44
  • I really don't see how this error could happen with the code you posted. Make sure everything is saved. Make sure to rebuild the project. Make sure you're not using some other service that you would import from another file. Commented Jul 2, 2017 at 15:52

2 Answers 2

2

Validate the function signature. Change

editEvent(eventId:number, event:Events){

to

editEvent(eventId:string, event:Events){
Sign up to request clarification or add additional context in comments.

1 Comment

I found what the issue was. There was another method with the same name editEvent and having a different method signature. I did not notice it. That was a stupid mistake. Thanks for the help.
0

I found what the issue was. There was another method with the same name editEvent and having a different method signature. I did not notice it. That was a stupid mistake. Thanks for the help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.