简体   繁体   English

Angular.subscribe 未显示错误/成功消息

[英]Angular .subscribe not showing error/success message

My angular frontend.subscribe is not console.log(errorMessage) or console.log(successMessage)... why?我的 angular frontend.subscribe 不是 console.log(errorMessage) 或 console.log(successMessage)... 为什么?

I need to push my error strings from backend java to angular frontend.我需要将我的错误字符串从后端 java 推送到 angular 前端。 The backend is logging errors correctly (on console) and all functionalities are working in frontend too,后端正确记录错误(在控制台上),所有功能也在前端工作,

But angular is not displaying my registration/login validation error strings at all.但是 angular 根本没有显示我的注册/登录验证错误字符串。 I suspect angular error/success message is null in frontend我怀疑前端的 angular 错误/成功消息是 null

Inspect chrome gives error code 500 and I need to narrow down where my error is... HELP!检查 chrome 给出错误代码 500,我需要缩小错误所在的范围......帮助!

Backend (Java) showing correct error message:显示正确错误消息的后端(Java):

n: CustomerService.EMAIL_ID_ALREADY_IN_USE
        at com.java.app2.service.CustomerServiceImpl.registerNewCustomer(CustomerServiceImpl.java:48) ~[classes/:na]
        at com.java.app2.service.CustomerServiceImpl$$FastClassBySpringCGLIB$$3b86d440.invoke(<generated>) ~[classes/:na]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.13.jar:5.3.13]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783) ~[spring-aop-5.3.13.jar:5.3.13]

chrome (inspect) showing pretty much nothing: chrome(检查)几乎什么也没显示:

zone.js:2863 POST http://localhost:3333/EKart_Server/customer-api/customers 500
...
...
Show 37 more frames
registration.service.ts:25 HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: 'OK', url: 'http://localhost:3333/EKart_Server/customer-api/customers', ok: false, …}
registration.component.ts:54 undefined

I think my error is here(component.ts):我认为我的错误在这里(component.ts):

    customer: Customer = new Customer
    registerUserForm!: FormGroup
    errorMessage: string = ''
    successMessage: string = ''
    constructor(private fb: FormBuilder, private registerService: RegistrationService) {}
    ngOnInit() {
        this.customer = new Customer()
        this.createForm()
    }
    registerUser() {
        this.errorMessage = ''
        this.successMessage = ''
        this.customer = this.registerUserForm.value as Customer

        this.registerService.registerCustomer(this.customer).subscribe(
            {
                next: message => {
                    this.successMessage = message
                    console.log(message)
                    console.log("meow meow")
                    this.registerUserForm.reset()

                },
                error: error => {
                    this.errorMessage = <any> error
                    console.log(error)
                }
            }
        )
    }

or maybe here(component.html)或者也许在这里(component.html)

<form [formGroup]="registerUserForm" (ngSubmit)="registerUser()">
...
validation stuff...
...
</form>
<h5 class="success-message">{{successMessage}}</h5>
<h5 class="error-message">{{errorMessage}}</h5>
<div class="text-info">Are you a registered user? <a routerLink="../login">Sign in</a></div>

If you send a String from your backend, then you need to tell to the httpClient the response type is going to be a text.如果您从后端发送一个字符串,那么您需要告诉 httpClient 响应类型将是一个文本。 By default it spects a json response.默认情况下,它会检测 json 响应。 So either you return a json response from api, or do this on your service:因此,要么您从 api 返回 json 响应,要么在您的服务上执行此操作:

getData(){
  return this.httpClient.get('whateverUrl', {responseType: 'text'});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM