Angular 角度2类型错误:无法读取属性';订阅';未定义的
我有一个表单,用于添加新事件或编辑现有事件。我检查URL参数,如果存在id,那么我希望从我的EventService订阅editEvent方法,否则从EventService订阅addEvent方法并添加新事件。添加一个事件可以正常工作,但如果在URL中传递了一个ID,则会出现类似无法读取未定义的属性“subscribe”的错误。我在我的editEvent方法中返回一个可观察对象(与addEvent中的相同),它没有嵌套在另一个调用中。下面是我的代码Angular 角度2类型错误:无法读取属性';订阅';未定义的,angular,observable,Angular,Observable,我有一个表单,用于添加新事件或编辑现有事件。我检查URL参数,如果存在id,那么我希望从我的EventService订阅editEvent方法,否则从EventService订阅addEvent方法并添加新事件。添加一个事件可以正常工作,但如果在URL中传递了一个ID,则会出现类似无法读取未定义的属性“subscribe”的错误。我在我的editEvent方法中返回一个可观察对象(与addEvent中的相同),它没有嵌套在另一个调用中。下面是我的代码 <form class="form-h
<form class="form-horizontal" [formGroup]="addEventForm" (ngSubmit)="addEvent()">
事件服务.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();
}
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()));
}
我是新来的。我尝试了所有在线可用的解决方案(比如使用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)
错误类型错误:无法读取未定义的属性“subscribe”
在EventEntryComponent.addEvent(eval at(bundle.js:901),:120:86)
在Object.eval[作为handleEvent](eventryComponent.html:3)
在handleEvent(评估处(bundle.js:132),:12120:138)
在callWithDebugContext(eval at(bundle.js:132),:13412:42)
在Object.debugHandleEvent[作为handleEvent](eval at(bundle.js:132),:13000:12)
在dispatchEvent(eval at(bundle.js:132),:9020:21)
评估时(评估时(bundle.js:132),:10948:20)
在SafeSubscriber.schedulerFn[as_next](eval at(bundle.js:132),:4069:36)
在保险箱订阅者处(评估地址:87),:238:16)
在SafeSubscriber.next(eval at(bundle.js:87),:185:22)
在订户处。下一步(在(bundle.js:87),:125:26处评估)
在Subscriber.next(eval at(bundle.js:87),:89:18)
在EventEmitter.Subject.next(eval at(bundle.js:151),:55:25)
在EventEmitter.emit(eval at(bundle.js:132),:4043:76)
在FormGroupDirective.onSubmit(eval at(bundle.js:434),:4859:23)
在Object.eval[作为handleEvent](eventryComponent.html:3)
在handleEvent(评估处(bundle.js:132),:12120:138)
在callWithDebugContext(eval at(bundle.js:132),:13412:42)
在Object.debugHandleEvent[作为handleEvent](eval at(bundle.js:132),:13000:12)
在dispatchEvent(eval at(bundle.js:132),:9020:21)
评估时(评估时(bundle.js:132),:9612:20)
在HTMLFormElement.eval(eval at(bundle.js:441),:2735:53)
在ZoneDelegate.invokeTask(eval at(bundle.js:4472),:424:31)
在Object.onInvokeTask(eval at(bundle.js:132),:4346:37)
在ZoneDelegate.invokeTask(eval at(bundle.js:4472),:423:36)
在Zone.runTask(eval at(bundle.js:4472),:191:47)
在HTMLFormElement.ZoneTask.invoke(eval at(bundle.js:4472),:486:38)
任何帮助都将不胜感激。验证函数签名。改变
editEvent(eventId:number, event:Events){
到
我发现了问题所在。还有另一个方法具有相同的名称editEvent,并且具有不同的方法签名。我没有注意到。那是个愚蠢的错误。感谢您的帮助。发布错误的完整堆栈跟踪。此外,您不需要字符串化正文和设置内容类型标题。我帮你做的,谢谢。添加了错误的完整堆栈跟踪。我真的不知道你发布的代码会发生这种错误。确保一切都已保存。确保重建项目。确保您没有使用从其他文件导入的其他服务。我发现了问题所在。还有另一个方法具有相同的名称editEvent,并且具有不同的方法签名。我没有注意到。那是个愚蠢的错误。谢谢你的帮助。
editEvent(eventId:string, event:Events){