0

I have a registration from in my angular project which sends the new user's details to the server and should get a response from it also, but instead it returns an error.

This is the registration service code:

const httpOptions = {
  headers: new HttpHeaders({
    "Content-Type": "application/json"
  })
};

@Injectable({
  providedIn: "root"
})
export class RegisterService {
  constructor(private http: HttpClient) {}

  addUser(user: registerForm): Observable<registerForm> {
    return this.http.post<registerForm>(
      `http://localhost:3000/users/register`,
      user,
      httpOptions
    );
  }
} 

Registration ts file:

export class RegisterPageComponent implements OnInit {
constructor(private rs: RegisterService, private registerForm: FormBuilder) {
    this.form = this.registerForm.group({
      email: "",
      password: "",
      fname: "",
      lname: "",
      city: "",
      adress: ""
    });
  }

  ngOnInit() {}
  getErrorMessage() {
    return this.email.hasError("required")
      ? "You must enter a value"
      : this.email.hasError("email")
      ? "Not a valid email"
      : "";
  }

  handSubmit(val) {
    console.log(val);
    this.rs.addUser(val).subscribe(data => {
      console.log(data);
    });
  }
}

and that's the error I get:

HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "Internal Server Error", url: "http://localhost:3000/users/register", ok: false, …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 500
statusText: "Internal Server Error"
url: "http://localhost:3000/users/register"
ok: false
name: "HttpErrorResponse"
message: "Http failure response for http://localhost:3000/users/register: 500 Internal Server Error"
error: "[email protected] is already taken by another user."
__proto__: HttpResponseBase
This is the error when status is 200:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:3000/users/register", ok: false, …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 200
statusText: "OK"
url: "http://localhost:3000/users/register"
ok: false
name: "HttpErrorResponse"
message: "Http failure during parsing for http://localhost:3000/users/register"
error: {error: SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse (<anonymous>) at XML

Maybe I'm missing something? at past I used to send requests and get responses by Fetch, then and catch. I can't understand where can I control the response from the server at the client side...

1
  • Change the e-mail id may be? the error message is pretty clear. Commented Feb 6, 2020 at 9:37

1 Answer 1

1

The error code is 500, meaning that the error is actually server side. The error is: [email protected] is already taken by another user.

try registering with another email. If that doesn't work, show us the backend registration code

Sign up to request clarification or add additional context in comments.

5 Comments

```HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "localhost:3000/users/register", ok: false, …} headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} status: 200 statusText: "OK" url: "localhost:3000/users/register" ok: false name: "HttpErrorResponse" message: "Http failure during parsing for localhost:3000/users/register" error: {error: SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse (<anonymous>) at XML" This is the error it send when status is 200
Ok perfect, now you got no error in your backend but the frontend is expecting a json response. Instead, your backend gave back something else. Can you check with the network tab in the developer tools of your browser what the response sent back by your backend server is?
it returned XHR
I need to see the content of the response in text format to help you further
it's actually only sends status of 200 by expressJS res.sendStatus(200); and If it's got error it's sends status of 500 with an error message res.status(500).send(err);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.