大家好,请您帮助我更新用户数据。我是刚接触到棱角分明和离子的人。我已经能够从本地存储中检索用户id,还编写了一个方法来输出用户的新数据,我遇到的问题是如何用新数据更新用户。这是我下面的代码。谢谢
// 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);
});
}
服务.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);
});
});
}
来自
this.WC.UpdateUser( isUserLoggedIn).then((data)=>{
console.log('update successful', data);
});
至
this.WC.UpdateUser( isUserLoggedIn, this.user).then((data)=>{
console.log('update successful', data);
});
和在服务文件中。
来自
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);
});
});
}
至
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);
});
});
}
不显示您的这个。WC.updateUser方法,不会给出正确的建议或答案。
请先发布,然后我们可以提供帮助。
简单来说,localStorage.setItem((、JSON.stringify((就足够了。然后,当你想把它拿出来的时候,你需要做相反的事情。