Solve the error of Cannot read properties of undefined (...) type

Scenes:

In the project, if you want to get the element of the mouse, the red area reports an error: Cannot read properties of undefined (reading 'grid3D')

analyze:

The Cannot read properties of undefined type of error is generally a problem with the previous element of the error element, that is, this.option is not obtained.

There are generally two types of errors:

  1. when the object has no value
  2. When the object is undefined

When the object has no data, it is undefined. At this time, an error will be reported when accessing the internal content.

Solution:

Take a look at this.option, comment out the problem code, and enter console.log(this.option)

let option = xxx;
this.rateChart.on("mouseover", (params) => {
        // console.log('params',params);
        if (params.target) {
          // console.log("非空白区");
        } else {
        //  console.log("空白区");
          console.log(this.option) 
          // this.option.grid3D.viewControl.alpha =20; // 视角绕 x 轴,即上下旋转的角度
          // this.option.grid3D.viewControl.beta = 70; // 视角绕 y 轴,即左右旋转的角度。
          // this.rateChart.setOption(this.option);
        }
}); 

It is found that the output is undefined, indicating that the option was not found. Check the context and find that the option has been defined before. You don’t need to use this, just use it directly.

After modification:

Successfully resolved.

Guess you like

Origin blog.csdn.net/qq_51978639/article/details/128827591