Magento – Error javascript define magento2 : window.checkout.quoteData or store code are undefined

magento2requirejs

I cannot inject some javascript dependencies of magento in my main.js.

ex:
'Magento_Checkout/js/action/get-payment-information',
'Magento_Checkout/js/model/totals'

Uncaught TypeError: Cannot read property 'quoteData' of undefined at quote.js:13

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

I see problem with window.checkoutConfig

Do you have an idea to fix that ?

define([
    "jquery",
    'Magento_Checkout/js/action/get-payment-information',
    'Magento_Checkout/js/model/totals'
],
function ($, getPaymentInformationAction, totals) {});

Best Answer

The issue is that both file don't have the correct information when the page is loaded, as this information is only available on the checkout page.

When i added in a JS file with your same code onto any page on the site, i get the same errors. When i add that same file into a template loaded on the checkout page, the errors goes away and i'm able to output the contents of both the getPaymentInformationAction and totals into the console.

define([
        "jquery",
        'Magento_Checkout/js/action/get-payment-information',
        'Magento_Checkout/js/model/totals'
    ],
    function ($, getPaymentInformationAction, totals) {
        console.log(getPaymentInformationAction);
        console.log(totals);
    });

enter image description here

Without a clear understanding of how and where you are trying to use this file, i can't say for sure how to implement these files correctly.