我的个人项目需要帮助。我正在尝试使用Angular HTTP post方法然后我遇到了这种错误。当我添加新数据,请致电addStudent
并重新加载页面时,semester
列会返回/显示[object Object]
。其他列正常工作,semester
除外。
//file name: app.service.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string): Observable<any>
{
return this.http.post("http://localhost:3000/Student", {id: newId, name: newName, year: newYear, semester: newSemester, score: newScore});
}
//file name : app.component.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string)
{
this.stockService.addStudent(newId, newName, newYear, newSemester, newScore).subscribe();
console.log(newSemester);
}
<!--file name: app.component.html-->
<tr *ngFor="let Student of Student" [attr.id]="Student.id">
<td>{{Student.id}}</td>
<td>{{Student.name}}</td>
<td>{{Student.year}}</td>
<td>{{Student.semester}}</td>
<td>{{Student.score}}</td>
</tr>
</table>
</h1>
<h3>ADD NEW</h3>
<br>ID: <input #newId/>
<br>Name: <input #newName/>
<br>Year: <input #newYear/>
<br>Semester: <input #newSemester/>
<br>Score: <input #newScore/>
<br><button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
当添加新行
时,Json看起来像这样 {
"id": "a",
"name": "a",
"year": "a",
"semester": {},
"score": "a"
}
如果需要更多代码段,请与我们联系。
谢谢。
答案 0 :(得分:1)
这只是错字。
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
看到你忘记了 .value for newSemester。
更改为:
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester.value, newScore.value)">ADD</button>