2

I'm using Magento 2.3.6 p1. And I want to observe the customers when they logged in. I used this in my Js file but it returned undefined when I console.log the first name value. It happened both when customers logged in and logged out.

define([
 "jquery", "Magento_Customer/js/customer-data"
], function(,ドル customerData) {
 "use strict";
 return function (config, element) {
 var firstname = customerData.get('customer')().firstname;
 console.log(firstname);
 };
});

I have no ideas why this happened. Because I can get the customer data in this way in the older version. Have Magento changed the way to do this, I'm confused.

asked Nov 15, 2021 at 2:59

1 Answer 1

1

You can get firstname using below code:

define([
 'uiComponent',
 'Magento_Customer/js/customer-data',
 'Magento_Customer/js/view/customer'
], function (Component, customerData) {
 'use strict';
 return Component.extend({
 /** @inheritdoc */
 initialize: function () {
 this._super();
 this.customer = customerData.get('customer');
 },
 /**
 * @return {*}
 */
 firstname: function() { 
 var customerInfo = customerData.get('customer')();
 var customerFirstname = customerInfo.firstname;
 return customerFirstname
 } 
 });
});

Then call firstname where you want to use it

answered Nov 15, 2021 at 7:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.