0

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?

1 Answer 1

1

self is undefined. Set var self = this at the begining of the getAllCustomers method.

1
  • Oh! It's a silly mistake by myself. Thank you so much.
    – Bojjaiah
    Commented Feb 27 at 11:24

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.