Reputation: 105
how can I get the user id that logged in into the system and put the user id in the payload if the user wants to update their data base on their id without the user put their id manually?
MY LOGIN COMPONENT
export class LoginComponent {
studentForm: FormGroup;
student: any;
constructor(
private fb: FormBuilder,
private crudService: CrudService,
private router: Router,
private toastr: ToastrService) {
this.studentForm = this.fb.group({
id: ['', Validators.compose([Validators.required])],
password: ['', Validators.compose([Validators.required])]
});
}
saveStudentDetails(values) {
const studentData = {};
studentData['id'] = values.id;
studentData['password'] = values.password;
this.crudService.loginstudent(studentData).subscribe(result => {
this.student = result;
this.toastr.success('You are logged in', 'Success !', { positionClass: 'toast-bottom-right' });
console.log(this.crudService.loginstudent);
this.router.navigate(['/address']);
},
err => {
console.log('status code ->' + err.status);
this.toastr.error('Please try again', 'Error !', { positionClass: 'toast-bottom-right' });
});
}}
// MY EDIT COMPONENT
saveStudentDetails(values) {
// const studentData = new FormData();
const studentData = {};
studentData['s_pNumber'] = values.s_pNumber;
studentData['s_address'] = values.s_address;
studentData['s_pCode'] = values.s_pCode;
studentData['s_city'] = values.s_city;
studentData['s_state'] = values.s_state;
studentData['s_country'] = values.s_country;
this.crudService.createAddress(studentData).subscribe(result => {
// this.student = result;
this.toastr.success('Your data has been inserted', 'Success !', { positionClass: 'toast-bottom-right' });
// this.router.navigate(['/finance']);
},
err => {
console.log('status code ->' + err.status);
this.toastr.error('Please try again', 'Error !', { positionClass: 'toast-bottom-right' });
});
}
// THIS IS MY SERVICE
createAddress(data) {
const postHttpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
postHttpOptions['observe'] = 'response';
return this.http.put(this.url + '/address/${id}', data);
}
I don't know if I'm doing the right thing, can anyone help me how to include the user id in the payload to be sent to backend? thank you
Upvotes: 0
Views: 387
Reputation: 1271
// Change this
return this.http.put(this.url + '/address/${id}', data);
//to this
return this.http.put(`${this.url}/address/${id}`, data);
//use the backtick
`
// not this
'
Upvotes: 1