uni-app Uncaught TypeError: Cannot read properties of null (reading 'bottom')

uni-app Uncaught TypeError: Cannot read properties of null (reading ‘bottom’)

项目名称 产品分类 开发环境 操作系统版本号 创建方式 CLI版本号
uniapp/H5 PC window10 CLI 2.0.1-34720220422002

操作步骤:

  • 在tabBar使用createIntersectionObserver时 切换页面会出现Uncaught TypeError: Cannot read properties of null (reading ‘bottom’)

预期结果:

  • 不会出现异常

实际结果:

  • 在tabBar使用createIntersectionObserver时 切换页面会出现Uncaught TypeError: Cannot read properties of null (reading ‘bottom’)

bug描述:

  • 在tabBar使用createIntersectionObserver时 切换页面会出现Uncaught TypeError: Cannot read properties of null (reading ‘bottom’)

Image

6 回复

请找出来具体原因后提供可复现bug的最小化demo(上传附件),让我们及时定位问题,及时修复。 【bug优先处理规则】https://ask.dcloud.net.cn/article/38139


已经找到问题了 是uview组件的问题 页面在后台时仍然在监听

回复 Thck: 大佬具体能说说是什么情况吗? 我也遇到这个报错, 不知道是哪里有问题

回复 Thck: (づ ̄ 3 ̄)づ

The error Uncaught TypeError: Cannot read properties of null (reading 'bottom') in a uni-app project typically occurs when your code is trying to access a property (in this case, bottom) of an object that is null. This usually happens when the object you’re trying to access hasn’t been properly initialized or doesn’t exist.

Here are some steps to debug and resolve this issue:

1. Identify the Source of the Error

  • Check the stack trace in the browser’s developer console to identify the exact line of code where the error is occurring.
  • Look for any code that is trying to access the bottom property of an object.

2. Check for Null or Undefined Objects

  • Ensure that the object you’re trying to access is not null or undefined before accessing its properties.
  • You can use optional chaining (?.) to safely access properties of potentially null or undefined objects.
const bottomValue = someObject?.bottom;

3. Verify Object Initialization

  • Make sure that the object is properly initialized before you try to access its properties.
  • If the object is supposed to be created by a function or API call, ensure that the function or API call has completed successfully.

4. Check for Asynchronous Code

  • If the object is being populated asynchronously (e.g., via an API call), ensure that you’re not trying to access it before the asynchronous operation has completed.
  • Use async/await or .then() to handle asynchronous operations properly.
async function fetchData() {
    const response = await someApiCall();
    const bottomValue = response.bottom;
}

5. Review Uni-App Specific Code

  • If the error is related to a uni-app specific component or API (e.g., uni.createSelectorQuery()), ensure that the component or API is being used correctly.
  • For example, if you’re using uni.createSelectorQuery(), make sure that the element you’re querying exists in the DOM.
uni.createSelectorQuery().select('.some-class').boundingClientRect(data => {
    if (data) {
        console.log(data.bottom);
    } else {
        console.error('Element not found');
    }
}).exec();

6. Add Defensive Programming

  • Add checks to ensure that the object exists before accessing its properties.
if (someObject && someObject.bottom !== undefined) {
    const bottomValue = someObject.bottom;
} else {
    console.error('someObject is null or does not have a bottom property');
}

7. Test and Debug

  • After making changes, test your application to ensure that the error is resolved.
  • Use console logs or debugger statements to inspect the state of the object at runtime.

Example Scenario

Suppose you have the following code:

const element = document.querySelector('.some-element');
console.log(element.bottom);

If .some-element does not exist in the DOM, element will be null, and trying to access element.bottom will throw the error. To fix this:

const element = document.querySelector('.some-element');
if (element) {
    console.log(element.bottom);
} else {
    console.error('Element not found');
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!