skip to Main Content

Good day everyone, Please I need your help on how to update a user data. I am new to angular and ionic. I have been able to retrieve user id from local storage, and also write a method that out the user new data, the problem I am having is how to update the user with the new data. This is my code below. Thanks.

// Update user info
  updateMethod(){

    // Retrieve user Id
    let isUserLoggedIn = localStorage.getItem('currentUserId');

    //this output user new info
    this.user = {
      first_name:this.user.first_name,
      last_name: this.user.last_name,
      username: this.user.username,
      email: this.user.email,
      billing: {
        phone: this.user.billing.phone,
        address_1: this.user.billing.address_1,
      }
   };
   console.log('new update', this.user);  
   //update user data
    this.WC.UpdateUser( isUserLoggedIn).then((data)=>{
         console.log('update successful', data);
       });

  }

service.ts

//siteUrl of the wordpress site
  siteUrl:string ='https://example.com';
  apiUrl:string ='';
  woocommercePath:string ='/wp-json/wc/v3/'
  wooPath:string ='/wp-json/wc/v2/'

  // woocommerce generated api keys
  consumerKey:string ='ck_xxxxxxxxxxxxxxx';
  consumerSecret:string ='cs_xxxxxxxxxxxxxx';


UpdateUser(userData){
  let headers = new HttpHeaders ({
    'Content-Type': 'application/x-www-form-urlencoded'
  });

  let usersData = this.JSON_to_URLEncoded(userData);

  this.apiUrl = `${this.siteUrl}${this.woocommercePath}customers/${userData}?consumer_key=${this.consumerKey}&consumer_secret=${this.consumerSecret}`;
  console.log('Update users data : ', this.apiUrl);


  return new Promise ((resolve) => {
    this.userupdate = this.http.put(this.apiUrl,usersData, {headers});
    this.userupdate.subscribe((responseData) => {
      resolve(responseData);
    });
  });

}

2

Answers


  1. Without showing your this.WC.updateUser method, no correct suggestion or answer will come.

    Please post that first then we can help out.

    In simple terms, localStorage.setItem(), JSON.stringify()) will suffice. Then when you want get it out you need to do the reverse.

    Login or Signup to reply.
  2. From

     this.WC.UpdateUser( isUserLoggedIn).then((data)=>{
             console.log('update successful', data);
           });
    

    To

     this.WC.UpdateUser( isUserLoggedIn, this.user).then((data)=>{
             console.log('update successful', data);
           });
    

    And in service file.

    FROM

    UpdateUser(userData){
      let headers = new HttpHeaders ({
        'Content-Type': 'application/x-www-form-urlencoded'
      });
    
      let usersData = this.JSON_to_URLEncoded(userData);
    
      this.apiUrl = `${this.siteUrl}${this.woocommercePath}customers/${userData}?consumer_key=${this.consumerKey}&consumer_secret=${this.consumerSecret}`;
      console.log('Update users data : ', this.apiUrl);
    
    
      return new Promise ((resolve) => {
        this.userupdate = this.http.put(this.apiUrl,usersData, {headers});
        this.userupdate.subscribe((responseData) => {
          resolve(responseData);
        });
      });
    
    }
    

    To

    UpdateUser(id,userData){
      let headers = new HttpHeaders ({
        'Content-Type': 'application/x-www-form-urlencoded'
      });
    
      let usersData = this.JSON_to_URLEncoded(userData);
    
      this.apiUrl = `${this.siteUrl}${this.woocommercePath}customers/${id}?consumer_key=${this.consumerKey}&consumer_secret=${this.consumerSecret}`;
      console.log('Update users data : ', this.apiUrl);
    
    
      return new Promise ((resolve) => {
        this.userupdate = this.http.put(this.apiUrl,usersData, {headers});
        this.userupdate.subscribe((responseData) => {
          resolve(responseData);
        });
      });
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search