Stack Overflow Asked by VijayT on January 29, 2021
I do not understand this error.
Is this a syntax error?
Is this happening because of the react-dom version?
Throwing error on this block.
getWitnessName = (witnessId) => {
if (this.props.witnesses) {
return this.props.witnesses.find(el => el.account_id === witnessId).account_name;
}
}
Error is :
Uncaught TypeError: Cannot read property 'account_name' of undefined
at BlockList.__getWitnessName__REACT_HOT_LOADER__ (BlockList.js:212)
at BlockList._this.getWitnessName (BlockList.js:76)
at eval (BlockList.js:321)
at Array.map (<anonymous>)
at BlockList.render (BlockList.js:314)
at finishClassComponent (react-dom.development.js:17160)
at updateClassComponent (react-dom.development.js:17110)
at beginWork (react-dom.development.js:18620)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
The above error occurred in the <BlockList> component:
in BlockList (created by Connect(BlockList))
in Connect(BlockList) (created by Route)
in Route
in Switch
in div
in Router (created by ConnectedRouter)
in ConnectedRouter
in Provider
in AppContainer
if you see the below code the witness var can be undefined if it doesn't find any account_id matching the given witnessId. In that case, the error is being thrown so do this
getWitnessName = (witnessId) => {
if (this.props.witnesses) {
const witness = this.props.witnesses.find(el => el.account_id === witnessId)
return witness ? witness.account_name : `No witness for given id: ${witnessId}`;
}
}
Answered by Basanta Kc on January 29, 2021
You can use Optional chaining
Add a nullish coalescing operator to complete the picture instead of the normal ||
which will not react to falsy values
return this.props.witnesses.find(el => el.account_id === witnessId)?.account_name ?? "N/A";
It will handle missing witness and missing account_name
Example
const props = {
witnesses: [
{ account_id: 1, account_name: "Fred"},
{ account_id: 2, account_name: "Joe" },
{ account_id: 3 }]
};
const getName = witnessId => props.witnesses
.find(el => el.account_id === witnessId)?.account_name ?? "N/A";
console.log(getName(1))
console.log(getName(2))
console.log(getName(3))
console.log(getName(4))
Answered by mplungjan on January 29, 2021
Try to validate first at to get the property, That's happening cause it's not finding any with that validation input. Then find returns undefined, and you can't obtain property from undefined.
For example
this.props.witnesses.find(el => el.account_id === witnessId)?.account_name ?? 'Account Not Found';
Answered by Edison Sanchez on January 29, 2021
getWitnessName = (witnessId) => {
if (this.props.witnesses) {
let foundWitnesses = this.props.witnesses.find(el => el.account_id === witnessId);
if(foundWitnesses)
return foundWitnesses.account_name;
else
return 'Not found';
}
}
Answered by Sunil Choudhary on January 29, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP