首页 > 解决方案 > 签名完成后在 activeuser 上接收 null

问题描述

我正在使用本地存储来存储用户名和密码 在这行代码之后我的活动用户为空,我不知道为什么

 console.log("I am the Active user: " + activeUser);
const menuChannelIcon = $(".menu-channel-icon");
const bellIcon = $(".bell-btn");
const uploadButtonIcon = $(".upload-btn");
const signInMainPageIcon = $(".signInMainPage");
const signUpMainPageIcon = $(".signUpMainPage");
const signOutMainPageIcon = $(".signOutMainPage");
const videoSectionMainPage = $(".video-section");

const signin = () => {
  console.log("signin button clicked")
  if (localStorage.getItem("formData")) {
    // as long as you are getting items from the local storage... do..
    JSON.parse(localStorage.getItem("formData")).forEach((data) => {

      let x = $("#signEmail").val();
      let z = $("#signinPassword").val();

      if (data.email == x && data.pwd == z) {
        isLogged = true;
        activeUser = localStorage.getItem(email);
        console.log("I am the Active user: " + activeUser);

        menuChannelIcon.show();
        bellIcon.show();
        uploadButtonIcon.show();
        signInMainPageIcon.hide();
        signUpMainPageIcon.hide();
        signOutMainPageIcon.show();
        singIn_formContainer.hide();
        videoSectionMainPage.show();
        console.log("You are LOGGED");
      }
    });
  }
};

编辑:在注册期间设置电子邮件和密码

标签: javascriptjquery

解决方案


Barmar 在评论中发布的答案:localStorage.getItem("email")

替代答案:当电子邮件和密码与 formData 中的某些内容匹配时,活动用户不应该是 x 吗?为什么要从本地存储中获取活动用户?


推荐阅读