Magento2.3 Undefined Error in window.checkout.quoteData – Ajax Cart Item Count Update

magento2magento2.3

I have created a custom page with free text ordering functionality and called custom add to cart API to add items to the cart.

Once the item is added I need to update the cart item count with updated quantity. I tried to use

require([
    'jquery',
    'Magento_Checkout/js/action/get-totals'
], function ($, getTotalsAction) {
    'use strict';

    var deferred = $.Deferred();
    getTotalsAction([], deferred);
});

But It is throwing error:
Uncaught TypeError: Cannot read property 'quoteData' of undefined at quote.js:34

And

url-builder.js:12 Uncaught TypeError: Cannot read property 'storeCode' of undefined at url-builder.js:12

Anything missing here?

I referred Error javascript define magento2 : window.checkout.quoteData or store code are undefined which doesn't have any working solutions.

Best Answer

I was able to achieve this my the below code. It might help someone

require([
       'Magento_Customer/js/customer-data'
    ], function (customerData) {
        var sections = ['cart'];
        customerData.invalidate(sections);
        customerData.reload(sections, true);
    });