Home > Blockchain >  Cannot find name `data`, but it's defined
Cannot find name `data`, but it's defined

Time:07-02

I have the following error: "Cannot find name 'data'.", but I have defined it.

const initialState = () => {
  try {
    const data = window.localStorage.getItem('auth');
  } catch (e) {
    const data = null;
  }

  if (!data) {
    return {
      matrixIsAuthenticated: false,
      matrixUserId: null,
      matrixAccessToken: null,
    };
  } else {
    return data;
  }
};

CodePudding user response:

You should define data variable outside try block first with let and give it value inside try only after that.

CodePudding user response:

First, you need to understand the scope of a block of code. Once you define a variable inside the try block it cannot be accessed outside of it. similar case for the catch also.

  • Related