LoginSignup
ryo-0213
@ryo-0213

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

jsonファイルのuserNameを表示させたい

jsonファイルのuserNameを表示させたい

発生している問題・エラーメッセージ

TypeError: Cannot read property 'toLowerCase' of null

原因だと思われるコード
色々と書き直しましたがうまく動きませんでした。

setOptions(
Object.keys(userprofiles).map(
(key) => userprofiles[key]
) as UserProfile[]
);

該当のソースコード

interface UserProfile {
  userName: string;

export default function Asynchronous() {
  const [options, setOptions] = useState<UserProfile[]>([]);

  useEffect(() => {
    let active = true;

    if (!loading) {
      return undefined;
    }

    (async () => {
      const response = await fetch("http://localhost:8080/api/v1/userprofile");
      await sleep(1e3);
      const userprofiles = await response.json();
      // keys(o: object): string[];
      if (active) {
        setOptions(
          Object.keys(userprofiles).map(
            (key) => userprofiles[key]
          ) as UserProfile[]
        );
      }
    })();
    return () => {
      active = false;
    };
  }, [loading]);

return (
    <Autocomplete
      id="demo"
      getOptionSelected={(option, value) => option.userName === value.userName}
      getOptionLabel={(option) => option.userName}
      options={options}
      loading={loading}
      renderInput={(params) => (
        <TextField
          {...params}
          label="検索"
          variant="outlined"
          InputProps={{
            ...params.InputProps,
            endAdornment: (
              <React.Fragment>
                {loading ? (
                  <CircularProgress color="inherit" size={20} />
                ) : null}
                {params.InputProps.endAdornment}
              </React.Fragment>
            ),
          }}
        />
      )}
    />
  );
}

補足情報(FW/ツールのバージョンなど)

"typescript": "^4.1.3",
"react": "^17.0.1",
"@material-ui/lab": "^4.0.0-alpha.57"

スクリーンショット 2020-12-30 19.49.09.png

jsonファイルの中身です

0

No Answers yet.

Your answer might help someone💌