Dory
Dory

Reputation: 105

Angular: Put logged in user id in the payload

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 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

Answers (1)

Sivuyile TG Magutywa
Sivuyile TG Magutywa

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
'

An image showing the backtick

Upvotes: 1

Related Questions