亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

呈現頁面時出現此錯誤

呈現頁面時出現此錯誤

至尊寶的傳說 2023-02-24 16:09:31
我不明白這個錯誤。這是語法錯誤嗎?發生這種情況是因為 react-dom 版本嗎?在此塊上拋出錯誤。getWitnessName = (witnessId) => {    if (this.props.witnesses) {        return this.props.witnesses.find(el => el.account_id === witnessId).account_name;    }}錯誤是:Uncaught TypeError: Cannot read property 'account_name' of undefinedat 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 Routein Switchin divin Router (created by ConnectedRouter)in ConnectedRouterin Providerin AppContainer
查看完整描述

4 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

如果你看到下面的代碼,如果它沒有找到與給定的 witnessId 匹配的任何 account_id,則 witness var 可以是未定義的。在這種情況下,會拋出錯誤,所以這樣做


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}`;

    }

}


查看完整回答
反對 回復 2023-02-24
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

您可以使用可選鏈接

添加一個nullish 合并運算符來完成圖片而不是正常的,||它不會對虛假值做出反應

return this.props.witnesses.find(el => el.account_id === witnessId)?.account_name ?? "N/A";

它將處理丟失的 witness 和丟失的 account_name

例子

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))



查看完整回答
反對 回復 2023-02-24
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

嘗試首先驗證以獲取屬性,這是因為它沒有找到任何驗證輸入。然后 find 返回 undefined,你不能從 undefined 中獲取屬性。

例如

this.props.witnesses.find(el => el.account_id === witnessId)?.account_name ?? 'Account Not Found';



查看完整回答
反對 回復 2023-02-24
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊



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';

    }

}


查看完整回答
反對 回復 2023-02-24
  • 4 回答
  • 0 關注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號