首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ionic中使用本地存储时出现问题

在ionic中使用本地存储时出现问题
EN

Stack Overflow用户
提问于 2019-01-24 19:41:54
回答 1查看 128关注 0票数 0

我要做的是-当一个函数被调用时,我在本地存储中设置(再次)一些键(在我的应用程序中的其他点设置,即Login )值对。类似于重置密码。它工作得很好,只是问题是,当我在同一个页面上,如果我访问这些项目后,重置它们,它显示未定义。但当我再次登录时,这些值会发生变化,并显示最新的值。

我也尝试过在android设备和ionic服务器和ionic实验室中运行。它从来没有起作用过。

代码语言:javascript
运行
复制
constructor(public navCtrl: NavController,
 public navParams: NavParams,
 public apiConnect:ApiIntegrationProvider,
 public loadingCtrl:LoadingController,
 public toastCtrl:ToastController,
  public storage: Storage) {

console.log(this.user_name)
this.fetchProfile();
 }

fetchProfile(){
this.user_role=localStorage.getItem("userRole");
this.user_name=localStorage.getItem("userName");
this.user_Email=localStorage.getItem("userEmail");
this.user_Mobile=localStorage.getItem("userMobile");
this.user_Avatar=localStorage.getItem("userAvatar");
this.user_Language=localStorage.getItem("userLanguage");
this.company_name=localStorage.getItem("companyName");
this.company_email=localStorage.getItem("companyEmail");
this.company_punch=localStorage.getItem("companyPunch");
this.company_url=localStorage.getItem("companyUrl");
console.log("user data")
console.log("company name", this.company_name,"email",this.company_email,"punch",this.company_punch,"website", this.company_url,"language", this.user_Language);
if(this.user_Language=='pt'){
  this.language="Portuguese"
}else{
  this.language="English"
}
if(this.user_role=='25'){ 
this.role='Inspector';

}else if(this.user_role=='35'){
this.role='Team Lead';
 }else if(this.user_role=='45'){
 this.role='Moderator';
}else if(this.user_role=='55'){
  this.role='Administrator';
}else if(this.user_role=='65'){
  this.role='Super Administrator';
}

}




 editProfile(){
console.log(this.decideEditProfile);
console.log("edit profile function")
this.decideEditProfile=true;

  }



  SaveProfile(){
    console.log(this.decideEditProfile);
    console.log("edit profile function")
    this.decideEditProfile=false;
    this.useredit();
  }

   ionViewDidLoad() {

  console.log('ionViewDidLoad ProfilePage');

  }
  showToast(position: string) {
    let toast = this.toastCtrl.create({
      message: this.editStatus.message,
      duration: 2000,
      position: 'top'
    });
    toast.present(toast);
  }
  useredit() {
    const loader = this.loadingCtrl.create({
      content: "Please wait...",
      duration: 3000
    });
    loader.present();
    let passingValue = {
      "user_id": '3',
      "inputName": 'James Red',
      "inputEmail":'moderator@gmail.com.in',
      "inputPassword":'123456'
    }
     this.apiConnect.postEditProfile( 
  JSON.stringify(passingValue)).subscribe((data) => {
  console.log("login api "+JSON.stringify(data));

  this.editStatus = data;
  if (this.editStatus.status == "success") {
    localStorage.removeItem("userName");
    localStorage.removeItem("userEmail");
    localStorage.removeItem("userMobile");
    localStorage.removeItem("userAvatar");
    localStorage.removeItem("userLanguage");
    localStorage.setItem("userName",this.editStatus.Profile.fullName);
    localStorage.setItem("userEmail",this.editStatus.Profile.emailAddress);
    localStorage.setItem("userMobile",this.editStatus.Profile.mobile);
    localStorage.setItem("userAvatar",this.editStatus.Profile.avatar);
    localStorage.setItem("userLanguage",this.editStatus.Profile.languagePreference);

    this.showToast('top');
    this.navCtrl.push("DashBoardPage");
  }
  else {

    this.showToast('top');
  }
  loader.dismiss();
})

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-25 15:32:29

从您的问题中,您试图说明即使在更新localstorage之后,您也会获得旧值或undefined。因此,您返回的数据可能为null或空。您必须仔细调试您的应用程序,因为localstorage的工作方式与此不同。执行控制台测试。

同样在这段代码中:

代码语言:javascript
运行
复制
if (this.editStatus.status == "success") {
    localStorage.removeItem("userName");
    localStorage.removeItem("userEmail");
    localStorage.removeItem("userMobile");
    localStorage.removeItem("userAvatar");
    localStorage.removeItem("userLanguage");
    localStorage.setItem("userName",this.editStatus.Profile.fullName);
    localStorage.setItem("userEmail",this.editStatus.Profile.emailAddress);
    localStorage.setItem("userMobile",this.editStatus.Profile.mobile);
    localStorage.setItem("userAvatar",this.editStatus.Profile.avatar);
    localStorage.setItem("userLanguage",this.editStatus.Profile.languagePreference);

    this.showToast('top');
    this.navCtrl.push("DashBoardPage");
  }

您不必删除不必要的项,只需设置localstorage,旧值将被新值替换。

我希望这对你有帮助。谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54345831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档