首页 文章

Angular 2 TypeError:无法读取未定义的属性'subscribe'

提问于
浏览
1

我有一个表单,用于添加新事件或编辑现有事件 . 我检查了URL参数,如果存在id,那么我想从我的EventService订阅editEvent方法,否则从EventService订阅addEvent方法并添加新事件 . 添加事件工作正常,而如果在URL中传递ID,则会收到类似 Cannot read property 'subscribe' of undefined 的错误 . 我在 editEvent 方法中返回一个observable(与addEvent中相同)并且它没有嵌套在另一个调用中 . 以下是我的代码 .

<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()));

    }

我是角度2的新手 . 我尝试了所有在线可用的解决方案(比如使用switchMap),但都没有用 . 错误的完整堆栈跟踪 .

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)

任何帮助,将不胜感激 .

2 回答

  • 0

    验证函数签名 . 更改

    editEvent(eventId:number, event:Events){
    

    editEvent(eventId:string, event:Events){
    
  • 2

    我发现了问题所在 . 还有另一种方法具有相同的名称editEvent并具有不同的方法签名 . 我没有注意到它 . 那是一个愚蠢的错误 . 谢谢您的帮助 .

相关问题