0

I get error:

Cannot read property 'addSuccessMessage' of undefined

When I want to add a success or error message via knockout js to magento 2. I did:

    define([
      'jquery', 'Magento_Ui/js/model/messageList', 'ko'
    ], function($) {
      "use strict";
      return function(config, element, messageList) {
        $(element).click(function() {
          var form = $(config.form);
          if ($('#h_estimate_amount').val()) {
            messageList.addSuccessMessage({
              message: 'Message to be shown.'
            });
          } else {
            messageList.addErrorMessage({
              message: 'Message to be shown.'
            });
            return false;
          }
        });
      }
    });

Any idea?

1 Answer 1

3

use code like this.

define([
      'jquery', 'Magento_Ui/js/model/messageList', 'ko'
    ], function($,messageList) {
      "use strict";
      return function(config, element, messageList) {
        $(element).click(function() {
          var form = $(config.form);
          if ($('#h_estimate_amount').val()) {
            messageList.addSuccessMessage({
              message: 'Message to be shown.'
            });
          } else {
            messageList.addErrorMessage({
              message: 'Message to be shown.'
            });
            return false;
          }
        });
      }
    });
1
  • Still return Uncaught TypeError: Cannot read property 'addErrorMessage' of undefined Nov 3, 2018 at 13:48

Your Answer

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

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