Closed Shanmukhsvstp closed 9 months ago
@Shanmukhsvstp the user
property is null when there's no user currently signed in on your app. Did you check if you are making the call to user.displayName
after the user has signed in?
I would recommend wrapping it in an if condition to help you debug:
const user = auth.currentUser;
if (user) {
const displayName = user.displayName;
const email = user.email;
const dpphotoURL = user.photoURL;
const uid = user.uid;
} else {
console.log("user is not logged in");
}
Hmm... Hello @thatfiredev, thanks for your quick reply. My actual usage of this profile data is in profile page. I actually use it as mentioned below:
<p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>Name: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userName'>{user.displayName}</p>
<br />
<p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>Email: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userEmail'>{user.email}</p><br />
<p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>User Id: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userID'>{user.uid}</p>
Here, I am facing problems with same error. Please help regarding it. Thankyou.
Update: Using {user?.displayName} instead or {user.displayName} fixed the error. Updated code:
<p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>Name: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userName'>{user?.displayName}</p> <br /> <p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>Email: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userEmail'>{user?.email}</p><br /> <p style={{display:'inline-block', marginRight:'10px', fontSize:"25px"}}><b>User Id: </b></p><p style={{display:'inline-block', fontSize:"25px"}} id='userID'>{user.?uid}</p>
I tried getting user info as below const user = auth.currentUser; const displayName = user.displayName; const email = user.email; const dpphotoURL = user.photoURL; const uid = user.uid;
but it shows an error that"
Unhandled Runtime Error TypeError: Cannot read properties of null (reading 'displayName')
Source pages\profile.jsx (17:22) @ user
15 | 16 |
and in console: Uncaught TypeError: Cannot read properties of null (reading 'displayName') at Profile (profile.jsx:17:23) at renderWithHooks (react-dom.development.js:16305:1) at mountIndeterminateComponent (react-dom.development.js:20074:1) at beginWork (react-dom.development.js:21587:1) at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1) at invokeGuardedCallback (react-dom.development.js:4277:1) at beginWork$1 (react-dom.development.js:27451:1) at performUnitOfWork (react-dom.development.js:26557:1) at workLoopConcurrent (react-dom.development.js:26543:1) at renderRootConcurrent (react-dom.development.js:26505:1) at performConcurrentWorkOnRoot (react-dom.development.js:25738:1) at workLoop (scheduler.development.js:266:1) at flushWork (scheduler.development.js:239:1) at MessagePort.performWorkUntilDeadline (scheduler.development.js:533:1)