Reputation:
I'm trying to verify that the response email field matches the email to the user which is logged-in. When I navigate to an URL (detail/:id) which was set up by another user and doesn't belong to the currently logged-in user, Angular should display an error message.
So far so good. But in my case, the else function, to display the correct response does not get executed. Also console.log(appointmentDetails);
doesn't give me any output.
I already tested the template view as I inverted the if-statement. So this would work.
UPDATE: The appointmentDetails = [0];
makes me confused.
Valid JSON response:
{
id: 241772331,
firstName: "Justin",
lastName: "Miller",
email: "[email protected]", ...
}
Error JSON response:
{
success: false,
message: 'You are not authorized to edit this apoointment.'
}
Template view:
<div class="row show-hide-message" *ngIf="message">
<div [ngClass]="messageClass">
{{ message }}
</div>
</div>
<div class="box">
{{ appointmentDetails.type }}
{{ appointmentDetails.firstName }}
{{ appointmentDetails.lastName }}
</div>
AppointmentDetailComponent:
export class AppointmentDetailComponent implements OnInit {
username = '';
email = '';
message;
messageClass;
getData: any;
appointmentDetails = [0];
id: number;
private sub: any;
constructor(
private authService: AuthService,
private apiService: ApiService,
private route: ActivatedRoute
) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id']; // (+) converts string 'id' to a number
});
this.getData = this.getDataFunction;
this.getData();
}
getDataFunction() {
this.authService.getProfile().subscribe(profile => {
this.username = profile.user.username; // Set username
this.email = profile.user.email; // Set e-mail
if (profile.user.email) {
console.log(this.id);
this.apiService
.getAppointmentDetailsById(this.id, this.email)
.subscribe(appointmentDetails => {
if (!appointmentDetails.success) {
this.messageClass = 'alert alert-danger'; // Set error bootstrap class
this.message = appointmentDetails.message; // Set error message
} else {
console.log(appointmentDetails);
this.appointmentDetails = appointmentDetails;
}
});
}
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
Server API route:
const url = process.env.acuityUri + '/appointments/' + id;
fetch(url)
.then(response => response.json())
.then(json => {
if (json.email === user.email) {
res.json(json);
} else {
res.json({
success: false,
message: 'You are not authorized to edit this apoointment.'
});
}
});
Upvotes: 0
Views: 132
Reputation: 2265
It seems you are just returning two different objects in the success and failure scenarios and not throwing any error. So you must do the check in the first block of subscribe
itself. No need to have the error
block. So look for the property
as a condition
So, use
this.apiService
.getAppointmentDetailsById(this.id, this.email)
.subscribe(appointmentDetails => {
if (!appointmentDetails.hasOwnProperty('success') {
this.messageClass = 'alert alert-danger'; // Set error bootstrap class
this.message = appointmentDetails.message; // Set error message
} else {
console.log(appointmentDetails);
this.appointmentDetails = appointmentDetails;
}
});
Upvotes: 0
Reputation: 2265
Try the following way,
this.apiService
.getAppointmentDetailsById(this.id, this.email)
.subscribe(appointmentDetails => {
console.log(appointmentDetails);
this.appointmentDetails = appointmentDetails;
}, (error) => {
this.messageClass = 'alert alert-danger'; // Set error bootstrap class
this.message = error.message;
});
Upvotes: 0