第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

關(guān)于ajax錯誤處理求解答!

關(guān)于ajax錯誤處理求解答!

藍(lán)山帝景 2019-07-26 23:27:19
代碼如下,其中有三個if判斷,來檢查數(shù)據(jù)是否符合預(yù)期,讓我困惑的是,這些檢查是否有必要?我是不是應(yīng)該拿掉這些判斷,讓其catch自行捕獲?哪種方式更合理些?或許還有更合理的做法?希望有人能夠指點下,謝謝asyncloadUserInfo(){try{const{data}=awaitgetUserInfo()if(data.status!==CONFIG.SUCCESS_CODE){return}if(!data.data.length){return}constaccount=util.getAccountById(data.data)if(!account||!account.userName||!account.userPhoneNo||!account.merchantName){return}this.setState({userName:account.userName,userPhoneNo:account.userPhoneNo,merchantName:account.merchantName,})}catch(err){alert('用戶信息數(shù)據(jù)發(fā)生錯誤')}}去掉檢查asyncloadUserInfo(){try{const{data}=awaitgetUserInfo()constaccount=util.getAccountById(data.data)this.setState({userName:account.userName,userPhoneNo:account.userPhoneNo,merchantName:account.merchantName,})}catch(err){alert('用戶信息數(shù)據(jù)發(fā)生錯誤')}}
查看完整描述

2 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗 獲得超5個贊

account如果不是空呢(null||undefined)?
上面和下面實現(xiàn)的功能不一樣。下面只能說account沒值,然后報錯。但是當(dāng)userName是空串的時候,無法做出應(yīng)對。UncaughtTypeError:Cannotreadproperty'userName'ofundefinedUncaughtTypeError:Cannotreadproperty'userName'ofnull
                            
查看完整回答
反對 回復(fù) 2019-07-26
?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗 獲得超4個贊

與@linong的看法不同我覺得只需要下面的ajax請求即可;如果account不是空,而是{},那么足以說明ajax請求是正常的,不正常的是服務(wù)器的數(shù)據(jù)這時候也不用報錯,可以在使用userName,userPhoneNo,merchantName數(shù)據(jù)時加上配置
姓名:{this.state.userName||'用戶名為空'}
//userPhoneNo,merchantName略
補充
this.setState({
userName:account.userName||'默認(rèn)值',
userPhoneNo:account.userPhoneNo||'默認(rèn)值',
merchantName:account.merchantName||'默認(rèn)值',
})
也可以如此吧,看使用場景
                            
                            
查看完整回答
反對 回復(fù) 2019-07-26
  • 2 回答
  • 0 關(guān)注
  • 348 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消