Question

I have found below error:
> Uncaught TypeError: Cannot read property 'set' of undefined.

Code:

    <script>
require(['jquery','Magento_Customer/js/customer-data','domReady!'], function ($, customerData) {

    $(function () {

        var settings = 
            {
                url: "<?php echo $block->getUrl('customdesignsubcategories/index/save'); ?>",
                method: "POST",
                allowedTypes: "pdf, ppt, pptx,doc, docx, jpg, jpeg, png, gif, xlsx, xlsm, xlsb, xls, xltx, xltm, xlt, csv, xlam, xla, ods, zip, txt",
                maxFileSize: 26214400,
                fileName: "myfile",
                uploadStr: jQuery(".box").html(),
                multiple: true,
                nestedForms:false,
                dragDropStr:"",
                onSuccess: function (files, data, xhr)
                {
                    var file_data = jQuery.parseJSON(data);                   
                    $('<input>').attr({
                        type: 'hidden',
                        name: file_data.original + "[]",
                        value: file_data.modified
                    }).appendTo('form');
                },
                onError: function (files, status, errMsg)
                {                    
                    $("#status").html(errMsg);
                }
            };
            $("#mulitplefileuploader").uploadFile(settings);
            var dataForm = $('#customdesignsubcategories_form');
            var ignore = null;

            dataForm.mage('validation', 
            {
                ignore: ignore ? ':hidden:not(' + ignore + ')' : ':hidden'
            }).find('input:text').attr('autocomplete', 'off');

            $('#finalSubmit').click( function() 
            { 
                $('.ajax-file-upload > form > input').unwrap(); 

                if(dataForm.validation('isValid') === false)
                {
                    $("#mulitplefileuploader").uploadFile(settings);
                    return false;
                }
                if(dataForm.validation('isValid')==true)    
                {
                    console.log(customerData);
                    $.ajax({
                        type:'POST',
                        url: '<?php echo $this->getUrl("customdesignsubcategories/index/sendEmail");?>',
                        data: dataForm.serialize(),
                        dataType:'json',
                        showLoader: true ,//use for display loader
                        success: function(data){

                            if(data.status == "CaptchaError")
                            {
                                $('#customdesignsubcategories_form .invalid-captcha').show();
                                $('#customdesignsubcategories_form').find('input[name*="captcha"]').focus();
                                return false;
                            }else if(data.status=="Success")
                            {
                                var msg = 'some message here';

                                customerData.set('messages', {
                                    messages: [{
                                        type: 'success',
                                        text: msg
                                    }]
                                });
                            }
                        }
                    });
                }
            });     
       });
   });
</script>
Was it helpful?

Solution

From magento2 phtml template you should do something like this:

<script>
require([
    'jquery',
    'Magento_Customer/js/customer-data',
    'domReady!'
], function ($, customerData) {
    $(function () {
        $.ajax({
            type:'POST',
            url: '<?php echo $this->getUrl("customdesignsubcategories/index/sendEmail");?>',
            data: dataForm.serialize(),
            dataType:'json',
            showLoader: true ,//use for display loader
            success: function(data){

                if(data.status == "CaptchaError")
                {
                    $('#customdesignsubcategories_form .invalid-captcha').show();
                    $('#customdesignsubcategories_form').find('input[name*="captcha"]').focus();
                    return false;
                }else if(data.status=="Success")
                {
                    var msg = 'some message here';

                    customerData.set('messages', {
                        messages: [{
                            type: 'success',
                            text: msg
                        }]
                    });
                }
            }
        });
    });
});

Using requirejs function to define jQuery and customerData.

OTHER TIPS

You have not create object of customerData and trying to call set function on it. That's why the error. Please create object of customerData first.

What is customerData for you? If you have created a function named customerData then you can call it like:

customerData (msg); // similarly send other arguments.

If you are usingcustomerData as a reference to Magento_Customer/js/customer-data then please use following pattern to define it as dependency: https://magento.stackexchange.com/a/237973/9169

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top