Skip to Content
Menu
This question has been flagged

In few product pages I am getting this error
UncaughtPromiseError > TypeError

Uncaught Promise > Cannot read properties of null (reading 'querySelectorAll')

TypeError: Cannot read properties of null (reading 'querySelectorAll')

    at Class.start (https://shop.testogcc.ae/web/assets/1/58ebb3c/web.assets_frontend_lazy.min.js:10652:2313)

    at prototype.<computed> [as start] (https://shop.testogcc.ae/web/assets/1/58ebb3c/web.assets_frontend_lazy.min.js:3372:516)

    at https://shop.testogcc.ae/web/assets/1/58ebb3c/web.assets_frontend_lazy.min.js:6166:13

    at async Promise.all (index 0)

    at async Promise.all (index 95)

    at async Promise.all (index 1)
And the JS is of 10K+ lines Can't understand how to fix it

Avatar
Discard

maybe are you creating a new view or template in Odoo?

If you are, can you share the code with us, it will be useful for more context.

Best Answer

The error "Cannot read properties of null (reading 'querySelectorAll')" typically occurs when JavaScript is attempting to select an element on the page using querySelectorAll, but the element does not exist in the DOM at the time the code runs. This issue is common in dynamically rendered pages like Odoo's frontend.

Here's how to troubleshoot and resolve this issue:

1. Understand the Error

  • The error indicates that querySelectorAll is being called on null, meaning the parent element or DOM element being queried is not present when the script runs.
  • Likely causes:
    • Missing or improperly loaded HTML elements.
    • JavaScript code executing before the DOM is fully rendered.
    • A condition in the template or JS code that fails to include the required element.

2. Identify the Source of the Problem

To pinpoint the issue, follow these steps:

a. Check the Line of Failure

  • The error occurs in:
    at Class.start (web.assets_frontend_lazy.min.js:10652:2313)
    
  • The start method is likely in a widget or class responsible for handling a frontend feature.

b. Unminify the Code

  • If possible, replace the minified web.assets_frontend_lazy.min.js with the original (unminified) JS file for debugging. Alternatively, use browser developer tools' "Pretty Print" feature to format the code.

c. Reproduce the Error

  • Open the product page where the error occurs.
  • Inspect the console to confirm the line and the conditions under which the error is thrown.

d. Look for the Selector

  • The issue may involve a specific widget or feature using a query like:
    const elements = this.$el[0].querySelectorAll('.some-class');
    
  • Check the .some-class in the page source to see if it exists.

3. Debugging the Issue

a. Validate the HTML Structure

  • Use browser developer tools (F12 in most browsers) to inspect the product page.
  • Ensure all required elements exist in the DOM before the script runs.
  • Look for:
    • Missing elements (e.g., .some-class).
    • Improperly loaded assets.

b. Check for Dynamic Rendering

  • If the page relies on AJAX or lazy loading, the script might run before the elements are available.
  • Wrap the script inside a DOMContentLoaded or equivalent callback to ensure it executes after the DOM is ready:
    document.addEventListener('DOMContentLoaded', function () {
        const elements = document.querySelectorAll('.some-class');
        if (elements) {
            // Your logic here
        }
    });
    

c. Handle Null Checks

  • Add checks in the JavaScript code to ensure the element exists before calling querySelectorAll:
    const parentElement = document.querySelector('.parent-class');
    if (parentElement) {
        const elements = parentElement.querySelectorAll('.some-class');
        // Further processing
    }
    

4. Fixing the Root Cause

a. Verify the Template

  • If the error occurs in a specific Odoo template, ensure the required elements are present in the QWeb template.
  • Example:
    <div class="parent-class">
        <div class="some-class">Content</div>
    </div>
    
  • Missing or conditionally excluded elements can cause this error.

b. Customize the Widget

  • If the issue is in an Odoo widget (e.g., in start()), override the widget to include a null check.
    Example:
    const MyWidget = Widget.extend({
        start: async function () {
            const parentElement = this.$el[0];
            if (parentElement) {
                const elements = parentElement.querySelectorAll('.some-class');
                // Your logic
            }
            return this._super(...arguments);
        },
    });
    

c. Update Odoo or Assets

  • If this is caused by a bug in Odoo's assets:
    • Update to the latest Odoo version to include any patches or fixes.
    • Rebuild the assets:
      ./odoo-bin -c odoo.conf --dev=assets
      

5. Advanced Debugging

a. Use Breakpoints

  • Set a breakpoint at the erroring line using browser developer tools.
  • Inspect the variables and DOM state at runtime to understand why the element is missing.

b. Disable Third-Party Modules

  • Temporarily disable custom or third-party modules that may modify product pages.
  • Test if the error persists with only Odoo's default configuration.

c. Rebuild Assets

  • Clear and regenerate frontend assets:
    ./odoo-bin -u all --dev=assets
    

6. Summary

  • This error is typically caused by missing elements or timing issues.
  • To resolve:
    1. Verify the DOM structure and ensure elements exist.
    2. Add null checks or delay the script execution until the DOM is ready.
    3. Customize the widget or template if needed.
    4. Update Odoo and rebuild assets to ensure you're using the latest fixes.

If the issue persists, provide additional details about the specific code or template causing the problem for more targeted assistance.

Avatar
Discard
Related Posts Replies Views Activity
1
Jan 23
23560
4
Mar 23
7215
1
May 21
3227
1
Feb 24
1602
0
Jun 23
1759