【发布时间】:2019-06-21 10:55:24
【问题描述】:
我有这两个类:
学生***1个地址
export class Student
{
id: number;
firstName: string;
adresse: Adresse;
}
export class Adresse
{
id: number;
email: string;
phone: string;
town: string;
city: string;
}
我去从list-student.component.ts
中选择一个学生<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Update</th>
</tr>
</thead>
<tbody *ngIf="!loader">
<tr *ngFor="let student of userbs | async" style="width: 1500px;">
<td>{{student.id}}</td>
<td>{{student.name}}</td>
<td><span class="button is-small btn-warning" (click)='editUser(student)'>Update</span></td>
</tr>
</tbody>
</table>
并且函数editUser 是在 modify-student.component.ts 中定义的:
editUser(student: Student): void
{
console.log("--------------Student's adress: " + --> It gives the error.
console.log("--------------Student's name: " + student.name); --> It gives the expected name for Example AJT_82 or Adrita.
this.router.navigate(['modify-student']);
};
我在尝试在console.log 上恢复学生的电子邮件时收到此错误。
ERROR TypeError: "student.adresse is undefined"
感谢@AJT_82 先生和@Adrita 夫人,我知道问题的原因。 由于从后端(Spring Boot)消耗休息服务而出现此错误。我添加注释
@JsonIgnore
private Adresse adresse;
这就是为什么在邮递员上,我无法显示学生内部的地址对象。
这就是为什么@JsonIgnore 导致我的地址未定义的原因。
但是由于消除了@JsonIgnore
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6].
您对解决这个问题有什么想法吗? 非常感谢。
【问题讨论】:
-
student和student.addresse和student.addresse.email有值吗?根据您显示的内容,我认为student是undefined。 -
如果您没有特定于类的方法,我会建议一个接口。但无论如何......这是 JS 类文档:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-
您好先生@AJT_82。非常感谢您的回复。我在list-students界面有学生列表,点击更新后,我要恢复选中学生的信息。我可以恢复名字,但我收到了上面附加的错误(student.adresse 未定义),请你看一下,我对我的问题做了一些更改。
-
嗯,错误图像与您在问题中写的完全不同...
-
在`editUser()`中请`console.log("--------------Student's :" + student.);并显示结果