AsyncStorage returning [SyntaxError: JSON Parse error: Unexpected character: o]

52 Views Asked by At

I am getting the above error when using AsyncStorage.

const defaultCity = {
    name: "Tbilisi",
    id: "22sH7BDcVH7MOYOtvqbD",
  };

  const [city, setCity] = useState(defaultCity);
  const [cityInitialized, setCityInitialized] = useState(false);

  const getStoredCity = async () => {
    try {
      const city = await JSON.parse(AsyncStorage.getItem("city"));
      if (!city) {
        setCityInitialized(true);
      } else {
        setCity(city);
        setCityInitialized(true);
      }
    } catch (error) {
      AsyncStorage.removeItem("city");
      console.log(error);
      setCity(defaultCity);
    }
  };

My object defaultCity should be able to be stringified unlessI'm missing something.

0

There are 0 best solutions below