node.js - 错误类型错误:无法读取未定义的属性“id”(AppComponent.html:34)
问题描述
我正在尝试nestJS
使用表单中的 HttpClient 和 [(ngModel)] 指令从 Angular 应用程序向服务器发出发布请求。当我提交表单时,我收到以下错误:
AppComponent.html:34 ERROR TypeError: Cannot read property 'id' of undefined
at AppComponent.addBook (app.component.ts:21)
at Object.eval [as handleEvent] (AppComponent.html:34)
at handleEvent (core.js:38098)
at callWithDebugContext (core.js:39716)
at Object.debugHandleEvent [as handleEvent] (core.js:39352)
at dispatchEvent (core.js:25818)
at core.js:37030
at HTMLButtonElement.<anonymous> (platform-browser.js:1789)
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
这是component.html
使用未定义变量的“id”属性的文件,它应该是真正的书籍对象:
//app.component.html
<form class="" action="addBook()" method="post">
<div class="form-group">
<label for="id">Book ID:</label>
<input id="id" name="bookID" type="text" [(ngModel)]="id" class="form-control">
<label for="title">Book Title:</label>
<input id="title" name="bookTitle" type="text" [(ngModel)]="title" class="form-control">
<label for="description">Book Description:</label>
<input id="description" name="bookDescription" type="text" [(ngModel)]="description" class="form-control">
<label for="author">Book Author:</label>
<input id="author" name="bookAuthor" type="text" [(ngModel)]="author" class="form-control">
<button type="submit" name="button" (click)="addBook()">add</button>
</div>
</form>
[(ngModel)] 指令中使用的所有属性都属于一个书籍对象,在app.component.ts
文件中定义:
//app.component.ts
import { Component } from '@angular/core';
import { ApiService } from './api.service';
import { Book } from './book';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
book: Book;
constructor(private apiService: ApiService){}
library = {};
ngOnInit(){
}
getBooks() {
this.apiService.getBooks().subscribe(data => this.library = data);
};
addBook(){
let book = {
id: this.book.id,
title: this.book.title,
description: this.book.description,
author: this.book.author
}
this.apiService.addBook(book).subscribe(
success => alert("Done"),
error => alert(error)
)
}
clearview() {
this.library = {}
};
}
这本书对象是 Book 类型,它是一个如下所示的类:
export class Book{
id: Number;
title: string;
description: string;
author: string;
}
抱歉,如果我把它弄得比需要的长,我想给出完整的上下文,因为我已经尝试了许多不同的方法来修复这个错误。
解决方案
将 app.component.html 更改为此:
<form class="" action="addBook()" method="post">
<div class="form-group">
<label for="id">Book ID:</label>
<input id="id" name="bookID" type="text" [(ngModel)]="book.id" class="form-control">
<label for="title">Book Title:</label>
<input id="title" name="bookTitle" type="text" [(ngModel)]="book.title" class="form-control">
<label for="description">Book Description:</label>
<input id="description" name="bookDescription" type="text" [(ngModel)]="book.description" class="form-control">
<label for="author">Book Author:</label>
<input id="author" name="bookAuthor" type="text" [(ngModel)]="book.author" class="form-control">
<button type="submit" name="button" (click)="addBook()">add</button>
</div>
</form>
阅读更多
推荐阅读
- spring-boot - AnsiOutput 颜色消息到控制台与文件
- javascript - ngRoute 不会正确加载模板
- r - 先验的数值矩阵
- git - 如何计算每个贡献者对 Git 或 Gerrit 中的代码的贡献百分比?
- java - Spark 在“org.codehaus.commons.compiler.CompileException”的日志中批量处理代码
- java - Android:许多按钮上的相同 OnClickListener
- android - RecyclerView:是否有用于处理项目点击的 OnItemActivatedListener 选项的替代方案?
- spring - 无法推断基本网址。这在使用动态 servlet 注册或 API 位于 API 网关后面时很常见
- c++ - 如何使用 lldb 和 VSCode 使调试器保持活动状态,以便与 bitcoind 和 bitcoin-cli 一起使用?
- rust - 在函数参数中,使用 ref 关键字和使用 & 符号有什么区别?