本文へスキップ
BecomeCoder

JavaScriptのエラー · エラー7

Cannot set properties of undefined ― 無いものに代入した

存在しない値(undefined)のプロパティに値を代入しようとすると出ます。ネストしたオブジェクトの、途中の階層を作り忘れているのが典型です。

出るコード

const config = {};
config.options.debug = true;   // options が無いので中に入れない

エラーメッセージ

TypeError: Cannot set properties of undefined (setting 'debug')

直し方

代入する前に、途中の階層を用意します。

const config = {};
config.options = {};
config.options.debug = true;