You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When making a call to the API using the endpoint: /user/:userId
Like: http://localhost:3000/user/18
The score value for the user “Karl” of ID 12 is stored inside a property of data named todayScore:
But the score value for the user “Cecilia” of ID 18 is stored inside a property of data named score:
So when we try accessing the value of the user's objective score, the property for either one of these users is undefined
For example:
//For Karl with an ID of 12 constkarlData=data;console.log(karlData?.todayScore);//→ Outputs 0.12 in the consoleconsole.log(karlData?.score);//→ Outputs undefined in the console[...]//For Cecicilia with an ID of 18constceciliaData=data;console.log(ceciliaData?.todayScore);//→ Outputs undefined in the consoleconsole.log(ceciliaData?.score);//→ Outputs 0.3 in the console
A fix would be to simply change the name property of either one of the mocked users
The text was updated successfully, but these errors were encountered:
As in real world you might not have an access to the api (ex: external api), a possible solution would be to create a single "todayScore" key and use the Nullish Coalescing Operator to set its value based on the available data:
When making a call to the API using the endpoint:
/user/:userId
Like:
http://localhost:3000/user/18
The score value for the user “Karl” of ID 12 is stored inside a property of data named
todayScore
:But the score value for the user “Cecilia” of ID 18 is stored inside a property of data named
score
:So when we try accessing the value of the user's objective score, the property for either one of these users is undefined
For example:
A fix would be to simply change the name property of either one of the mocked users
The text was updated successfully, but these errors were encountered: