【问题标题】:Unfefined status using a class field in Angular使用 Angular 中的类字段未定义的状态
【发布时间】:2021-10-29 02:45:40
【问题描述】:

问题是关于在 html 视图或控制台中显示类的字段。 我的配置:

export class UserDataService {

  constructor(
    private http:HttpClient
  ) { }

  executeHelloUserBeanService(){
    return this.http.get<User>("http://localhost:8081/physio-node/test/admin");
  }
}

从 Spring 传递过来的对象:

public class UserDTO {
    public String username;
    public String email;

    public UserDTO(User user) {
        this.username = user.getUserName();
        this.email = user.getUserE_mail();
    }
}

Angular 中的用户模型:

export class User {
 public username: string;
 public email: string;

 
    constructor(username: string, email: string){
      this.username = username;
      this.email = email;
    }
  }

export class ProfileComponent implements OnInit {
  user: User;
  name: string = "true";

  constructor(
    private service:UserDataService
  ) { }

  ngOnInit(): void {
    this.refreshProfile();
  }

   refreshProfile(){
    this.service.executeHelloUserBeanService().subscribe(
      response => {
        this.user = response;
        console.log(this.user);
        console.log(this.user.username);
      }
    )
  }

所以, console.log(this.user) 似乎是正确的,因为我可以在控制台中看到完整的对象

但是当我尝试在 html 视图中显示 {{user.username}} 时,它不起作用并且控制台:console.log(this.user.username) 显示:未定义;

@编辑

public class UserService {

    private UserTaskRepository userTaskRepository;

    public UserService(UserTaskRepository userTaskRepository) {
        this.userTaskRepository = userTaskRepository;
    }

    public UserDTO getUserbyID(String username){
        return userTaskRepository.findByUserName(username)
                .stream().
                        map(UserDTO::new).
                        collect(Collectors.toList()).get(0);
    }




}

【问题讨论】:

  • 你能在控制台登录this.user后添加截图吗?也许响应结构不同。
  • 已添加屏幕截图。

标签: angular spring


【解决方案1】:

来自服务器的响应似乎是一组用户,而不是您期望的单个用户。您可以在后端进行更正,也可以从响应数据中提取第一个元素:

executeHelloUserBeanService(){
    return this.http.get<User[]>("http://localhost:8081/physio-node/test/admin").pipe(
      // make sure the array is there and it has at least 1 element
      filter(res => res && res.length > 0),
      // extract the first element
      map(res => res[0])
    );
  }

【讨论】:

  • 像我这样改变getUserbyID方法是个好习惯吗?
  • 尽管这更像是一个 java 问题,而不是一个与角度相关的问题,但我还是会选择 findFirst docs.oracle.com/javase/8/docs/api/java/util/stream/…return userTaskRepository.findByUserName(username).stream().map(UserDTO::new).findFirst()(而不是将结果收集到列表中)。
【解决方案2】:

在附加的屏幕截图中,很明显响应不是单一的object,而是User Objectarray。因此,要么您必须更改后端的实现,例如 return single object 而不是 array,要么像这样更改此 refreshProfile() 方法的实现

refreshProfile(){
    this.service.executeHelloUserBeanService().subscribe(
      response => {
        this.user = response[0]; // just read first element from response
        console.log(this.user);
        console.log(this.user.username);
      }
    )
  }

【讨论】:

  • 像我一样改变getUserbyID方法是个好习惯吗? (方法,添加到帖子中)
  • 您正在使用 Java Spring Boot 作为后端,对吗?我不太了解 Spring Boot,但我知道 Spring Boot 已经提供了获取单个对象的内置方法,所以如果你遵循最佳实践会很好
猜你喜欢
  • 2016-05-28
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2020-03-14
  • 2021-11-09
相关资源
最近更新 更多