undefinedのプロパティにアクセスしたときのエラーです。オプショナルチェーンで安全に書けます。
JSONのデータとアクセスしたいパス(例: user.profile.name)を入力すると、パスのどこで undefined になるかを特定します。データは送信されません。
const user = {};
console.log(user.profile.name);
const user = {};
console.log(user.profile?.name); // undefined(エラーにならない)
console.log(user.profile?.name ?? "名無し");
▶ 修正済みコードを実行エディタで開く
途中の値がundefinedになり得るときはオプショナルチェーン?.を使います。user.profile?.nameならprofileが無くてもエラーにならずundefinedを返します。既定値は?? "名無し"で補えます。
(reading 'name') の 'name' は、アクセスしようとしたプロパティ名です。つまりその手前の値が undefinedということ。user.profile.name でこの表示が出たなら、user.profile が undefined です。まずそこを疑います。
なお Cannot read property 'name' of undefined(単数形・reading 無し)は古いブラウザ/Node の表示で、原因と対処は同じです。
const found = users.find(u => u.id === 99); // 見つからないと undefined
console.log(found?.name ?? "該当なし");
const res = await fetch("/api/user");
const data = await res.json();
console.log(data.items?.[0]?.name); // items がまだ無くても安全
// 取得前は user が undefined
if (!user) return <p>読み込み中…</p>;
return <p>{user.name}</p>;
const { name = "名無し" } = user ?? {};
. の左側を console.log して、どれが undefined か確認。