Skip to content

getting error “Cannot read properties of undefined (reading ‘push’)” for knockout observable variable

I just called the custom method inside the knockout js initialize function to show the list of customers by default in my custom module. I can return the data, but I’m getting the error “Cannot read properties of undefined (reading ‘push’)“.

Here is my code:

define([
    'ko',
    'uiComponent',
    'mage/url',
    'mage/storage',
    'jquery'
], function (ko, Component, urlBuilder,storage, $) {
    'use strict';

    return Component.extend({

        defaults: {
            template: 'ABCSolutions_Customer/test',
        },

        customerList: ko.observableArray([]),

        initObservable: function () {
            this._super();
            return this;
        },

        initialize: function () {
            this._super();
            this.getAllCustomers();
        },


        getAllCustomers: function () {

            debugger;

            let serviceUrl = urlBuilder.build('customers/index/allcustomers');
             

            return storage.post(
                serviceUrl,
                ''
            ).done(
                function (response) {
                    debugger;
                    let customerLstObject = JSON.parse(response);
                    if(typeof customerLstObject != 'undefined' && customerLstObject != null && customerLstObject.length > 0){
                        for (let i = 0; i<customerLstObject.length; i++){
                            self.customerList.push(customerLstObject[i]);
                        }
                    }
                }
            ).fail(
                function (response) {
                    alert(response);
                }
            );
        },

        getSearchCustomers: function () {                
            ..
        },

    });
});

When I use push method from the self.customerList.push() line getting error.

Any help on this?