我有一个Angular 6前端,当使用Spring Boot和PostgreSQL单击页面上的submit
按钮到后端时,它发出POST请求(用户名)。返回的数据是该用户名的电子邮件地址。
何时使用.subscribe()
,我必须按两次提交按钮,直到收到电子邮件为止(在后端,即使是第一次单击,它也会打印到控制台上)。
但是,当我使用.pipe(tap(...)).subscribe()
时,我得到的是预期的结果(一击)。
使用订阅
reset(username:string) {
console.log(username);
this.http.post('http://localhost:8090/reset', username, this.httpOptions).subscribe( data=> {
this.emails = data as any;
console.log(this.emails);
});
}
使用点击(插入)
reset(username:string) {
console.log(username);
this.http.post('http://localhost:8090/reset', username, this.httpOptions).pipe(tap((data) => {
this.emails = data as any;
console.log(this.emails);
})).subscribe();
}
Spring Boot
@RequestMapping(value = "/reset", method = RequestMethod.POST)
public Email resetMail(@RequestBody String username) {
try{
User user = userService.findByUsername(username);
System.out.println(user.getEmail());
Email emails = new Email(user.getEmail());
return emails;
}catch(Exception e) {
System.out.println("Not Present");
Email emails = new Email("Not Present");
//e.printStackTrace(); Prints out NullException StackTrace.
return emails;
}
}