Skip to content

Error in .on('rulesChanged.queryBuilder') function when calling getRules #833

Open
@cormip

Description

@cormip

Plugin initializes normally, and can load previously saved rules at initialization without problem.

I've added an auto-save feature using the "rulesChanged.queryBuilder API Event as follows:

$('#queryBuilder').on('rulesChanged.queryBuilder', function(evt) {
        const blockRules = $('#queryBuilder').queryBuilder('getRules')
        console.log('Rule Changed!', blockRules)
        if (blockRules) {
            //Save the rule
            //saveRules function here
        }
    })

The problem arises if I try to clear or change the ruleset on an already initialized queryBuilder:

$('#queryBuilder').queryBuilder('clear')

or

$('#queryBuilder').queryBuilder('setRules', rules, true)

Either of the calls above will trigger the rulesChanged event, resulting in the getRules method crashing with the error:

/node_modules/jQuery-QueryBuilder/dist/js/query-builder.js:1697 Uncaught TypeError: Cannot read property 'each' of null at parse (/node_modules/jQuery-QueryBuilder/dist/js/query-builder.js:1697) at QueryBuilder.validate (/node_modules/jQuery-QueryBuilder/dist/js/query-builder.js:1749) at QueryBuilder.getRules (/node_modules/jQuery-QueryBuilder/dist/js/query-builder.js:1779)

I can probably work around it by adding an ugly try/catch or using beforeClear to destroy the listener and rebuild it again, but there really should be an integrated solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIdentified bug which needs a fix

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions