2

I have created a custom js for customer add form. But I am getting the error "can not read property 'get' of undefined" in console.

Please help. Thanks in advance.

Vendor/CustomCustomer/view/base/web/customercustom.js

define([
    "jquery",
    'jquery/ui',
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'mage/url'
], function (_, uiRegistry, Select) {
    'use strict';

    return function (Select) {
        return Select.extend({
            initialize: function (value) {
                this._super();
                var customerCompany = uiRegistry.get('index = customer_company');
                var defaultCompany = uiRegistry.get('index = extension_attributes.company_attributes.company_id');
                var vessels = uiRegistry.get('index = customer_vessels');
                var customerUsername = uiRegistry.get('index = customer_username');
                var password = uiRegistry.get('index = password');
                var cnfpassword = uiRegistry.get('index = confirm_password');
                if (this.value() != 1) {
                    customerCompany.show();
                    defaultCompany.hide();
                    vessels.show();
                    customerUsername.show();
                    password.show();
                    cnfpassword.show();
                } else {
                    customerCompany.hide();
                    defaultCompany.show();
                    vessels.hide();
                    customerUsername.hide();
                    password.hide();
                    cnfpassword.hide();
                }
                return this;
            },
            onUpdate: function (value) {
                var customerCompany = uiRegistry.get('index = customer_company');
                var defaultCompany = uiRegistry.get('index = extension_attributes.company_attributes.company_id');
                var customerUsername = uiRegistry.get('index = customer_username');
                var password = uiRegistry.get('index = password');
                var cnfpassword = uiRegistry.get('index = confirm_password');
                if (value != 1) {
                    customerCompany.show();
                    defaultCompany.hide();
                    customerUsername.show();
                    password.show();
                    cnfpassword.show();
                } else {
                    customerCompany.hide();
                    defaultCompany.show();
                    customerUsername.hide();
                     password.hide();
                     cnfpassword.hide();
                }

                return this._super();
            },
        });
    }
});

1 Answer 1

0

Change position of jquery components in define() as below:

define([
    'jquery',
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'jquery/ui',
    'mage/url'
], function ($, _, uiRegistry, Select) {
    'use strict';

    return function (Select) {
        return Select.extend({
            initialize: function (value) {
                this._super();
                var customerCompany = uiRegistry.get('index = customer_company');
                var defaultCompany = uiRegistry.get('index = extension_attributes.company_attributes.company_id');
                var vessels = uiRegistry.get('index = customer_vessels');
                var customerUsername = uiRegistry.get('index = customer_username');
                var password = uiRegistry.get('index = password');
                var cnfpassword = uiRegistry.get('index = confirm_password');
                if (this.value() != 1) {
                    customerCompany.show();
                    defaultCompany.hide();
                    vessels.show();
                    customerUsername.show();
                    password.show();
                    cnfpassword.show();
                } else {
                    customerCompany.hide();
                    defaultCompany.show();
                    vessels.hide();
                    customerUsername.hide();
                    password.hide();
                    cnfpassword.hide();
                }
                return this;
            },
            onUpdate: function (value) {
                var customerCompany = uiRegistry.get('index = customer_company');
                var defaultCompany = uiRegistry.get('index = extension_attributes.company_attributes.company_id');
                var customerUsername = uiRegistry.get('index = customer_username');
                var password = uiRegistry.get('index = password');
                var cnfpassword = uiRegistry.get('index = confirm_password');
                if (value != 1) {
                    customerCompany.show();
                    defaultCompany.hide();
                    customerUsername.show();
                    password.show();
                    cnfpassword.show();
                } else {
                    customerCompany.hide();
                    defaultCompany.show();
                    customerUsername.hide();
                     password.hide();
                     cnfpassword.hide();
                }

                return this._super();
            },
        });
    }
});
3
  • Thanks for the response. After making the abover mentioned changes now I am getting the error "Cannot read property 'show' of undefined". Commented Apr 22, 2019 at 9:50
  • it means selector on which you are using show() method is undefine, means you didnt get selector using uiRegistry Commented Apr 24, 2019 at 4:50
  • But it works on the second reload. Commented Apr 24, 2019 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.