title: How do i solve a localStorage.getItem() error?
body: <p>I am trying to get a user's data when he/she logs into a database (firestore), using a form. I am using the JSON.parse(localStorage.getItem()) method to get the user's data when he logs in or return null when the user logs out. So I wrote this inside my code:</p>
  <pre><code>localStorage.setItem('user', JSON.stringify(this.userData));
          JSON.parse(localStorage.getItem('user'));
        } else {
          localStorage.setItem('user', null);
          JSON.parse(localStorage.getItem('user'));
        }
  </code></pre>
  <p>But i keep getting this error which says &quot;method ...localStorage.getItem() has an argument type of 'string | null'...&quot;</p>
  <p>is there something i am missing?</p>
  <p>Below is my full code:</p>
  <pre><code>import { Injectable, NgZone } from '@angular/core';
  import { Webuser } from './webuser';
  import { AngularFireAuth } from '@angular/fire/auth';
  import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
  import { Router } from '@angular/router';
  //import * as firebase from 'firebase/app';
  //import 'firebase/auth';
  
  
  @Injectable({
    providedIn: 'root'
  })
  
  export class AuthService {
    userData: any; // This saves logged in user data
  
    constructor(
      public afs: AngularFirestore, // Inject Firestore service
      public afAuth: AngularFireAuth, // Inject Firebase auth service
      public router: Router,
      public ngZone: NgZone // NgZone service is called to remove outside scope warnings
      ) {
         /* Saving user data in local storage when 
           logged in and setting up null when logged out */
  
      this.afAuth.authState.subscribe(user =&gt; {
        if (user) {
          this.userData = user;
          localStorage.setItem('user', JSON.stringify(this.userData));
          JSON.parse(localStorage.getItem('user'));
        } else {
          localStorage.setItem('user', null);
          JSON.parse(localStorage.getItem('user'));
        }
      })
  
     }
  
     // Login with email/password
     Login(email: string, password: string) {
       return this.afAuth.signInWithEmailAndPassword(email, password)
       .then((result) =&gt; {
         this.ngZone.run(() =&gt; {
           this.router.navigate(['']);
         });
         this.SetUserData(result.user);
       }).catch((error) =&gt; {
         window.alert(error.message);
       })
     }
  
     // Signup with email/password
     SignUp(email: string, password: string) {
      return this.afAuth.createUserWithEmailAndPassword(email, password)
      .then((result) =&gt; {
        // Here we call the SendVerificationMail() method when a user signs up and returns promise
        this.SendVerificationMail();
        this.SetUserData(result.user);
      }).catch((error) =&gt; {
        window.alert(error.message);
      })
     }
  
     // Send email verification when user signs up
     SendVerificationMail() {
       return this.afAuth.currentUser.then(u =&gt; u.sendEmailVerification())
       .then(() =&gt; {
         this.router.navigate(['verify-email-address']);
       })
     }
  
     // Reset Forgotten password
     ForgotPasword(passwordResetEmail: string) {
       return this.afAuth.sendPasswordResetEmail(passwordResetEmail)
       .then(() =&gt; {
         window.alert('Password reset email sent to the email you provided. Please check your inbox');
  
       }).catch((error) =&gt; {
         window.alert(error);
       })
     }
  
     // This returns true when user email is verified and user is logged in
     get isLoggedIn(): boolean {
       const user = JSON.parse(localStorage.getItem('user'));
       return (user !== null &amp;&amp; user.emailVerified !== false)
     }
  
     /* Setting up user data when login with username/password, 
        sign up with username/password in Firestore database using 
        AngularFirestore + AngularFirestoreDocument service */
  
        SetUserData(user: any) {
          const userRef: AngularFirestoreDocument&lt;any&gt; = this.afs.doc('users/${user.uid}');
          const userData: Webuser ={
            uId: user.uid,
            name: user.name,
            emailVerified: user.emailVerified
          }
          return userRef.set(userData, {
            merge: true
          })
        }
  
        // Signing out a user
  
        SignOut() {
          return this.afAuth.signOut().then(() =&gt; {
            localStorage.removeItem('user');
            this.router.navigate(['login']);
          })
        }
  }
  </code></pre>
  <p>What am I doing wrong?</p>
  <p>I followed the example given here: <a href="https://www.positronx.io/full-angular-7-firebase-authentication-system/" rel="nofollow noreferrer">https://www.positronx.io/full-angular-7-firebase-authentication-system/</a></p>