I have been facing this issue for quite some time now. The backend API response is indicating that a certain property does not exist, even though it clearly does. My Angular application suddenly started showing 18 errors today, and I am at a loss on how to resolve this "issue."
import { Component, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';
import { TokenService } from '../../authentication/services/token.service';
import { HttpErrorResponse } from '@angular/common/http';
import { AdminService } from '../../authentication/services/admin.service';
import { CrudService } from '../services/crud.service';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css'],
})
export class UsersComponent implements OnInit {
constructor(
private _token: TokenService,
private _router: Router,
private _admin: AdminService,
private _crud: CrudService
) {}
adminId: string;
adminName: string;
adminEmail: string;
users: any[];
deletedId: string;
successMessage: string;
p: number = 1;
loading: string;
ngOnInit(): void {
this.loading = 'true';
this._token.verifyToken().subscribe(
(res) => {
this.adminId = res.admin._id;
localStorage.setItem('adminid', this.adminId);
this._admin.getAdminById(this.adminId).subscribe((res) => {
this.adminName = res.admin.name;
this.adminEmail = res.admin.email;
this._crud.readAllUsers().subscribe((res) => {
this.users = res.users;
setTimeout(() => {
this.loading = '';
}, 950);
});
});
},
(err) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 400) {
this._router.navigate(['/login']);
}
}
}
);
}
deleteUserById(id: string) {
this._crud.deleteUser(id).subscribe(() => {
this._crud.readAllUsers().subscribe((res) => {
this.users = res.users;
this.successMessage = 'User successfully deleted';
});
});
}
removePopup() {
this.successMessage = '';
}
}