AsyncStorage 返回 [SyntaxError: JSON Parse error: Unexpected character: o]

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

提问人:David Henry 提问时间:10/12/2023 更新时间:10/12/2023 访问量:18

问:

我在使用 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);
    }
  };

我的对象应该能够被字符串化,除非我遗漏了一些东西。defaultCity

JSON 解析 AsyncStorage Stringify

评论


答: 暂无答案