0
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>
  • Can you give me more information regarding this issue? – Ajithkumar Dec 13 '18 at 5:27
  • On submit of form i want to display success message using customer-data.js but reflects the error of undefined method "set" as u can see i have defined in code – Parthavi Dec 13 '18 at 5:31
  • can you try same script in phtml file (i.e below that form) – Ajithkumar Dec 13 '18 at 5:34
  • Yes i have put the script in phtml – Parthavi Dec 13 '18 at 5:34
1

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.

  • I have used the same code but still issue found – Parthavi Dec 13 '18 at 10:00
  • Check if customerData is there: console.log(customerData); – gemig_hol Dec 13 '18 at 10:46
  • Found undefined – Parthavi Dec 13 '18 at 10:56
  • I get the whole object, try clearing static folder and try in your chrome console: require('Magento_Customer/js/customer-data'); – gemig_hol Dec 13 '18 at 11:17
  • please check my edited code? – Parthavi Dec 13 '18 at 11:29
0

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

  • Use the same code is there any other way to solve issue? – Parthavi Dec 13 '18 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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