Uncaught TypeError undefined has no properties
This error typically occurs when you try to access a property or method of an undefined value. For example:
var myObject;
console.log(myObject.property); // Uncaught TypeError: undefined has no properties
In this case, myObject is undefined, so trying to access its property property results in an error.
To avoid this error, you should check whether the value is defined before trying to access its properties or methods:
var myObject;
if (myObject !== undefined) {
console.log(myObject.property);
}
Alternatively, you can use the optional chaining operator ?. to access properties and methods of potentially undefined values without throwing an error:
var myObject;
console.log(myObject?.property); // undefined
This will return undefined if myObject is undefined, instead of throwing an error
原文地址: https://www.cveoy.top/t/topic/fjOE 著作权归作者所有。请勿转载和采集!