一、錯誤查找
問題描述:前端一個el-table表格,一個醫(yī)院查詢到的科室從后端返回時總是顯示不出來,response里面是有數(shù)據(jù)的,這個表格別的醫(yī)院都能顯示出科室,就那個醫(yī)院顯示不出。報錯:TypeError: Cannot read properties of undefined (reading 'NAME')
?查找問題所在,發(fā)現(xiàn)el-table里面有一個:formatter="formatter_DepType",具體方法如下:
const formatter_DepType = function (row, column) {
if (_this.Type_Ary && row.DEP_TYPE) {
return _this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME;
}
return "";
}
其中Type_Ary里面的內(nèi)容是這樣的:
Type_Ary: [{ CODE: '1', NAME: '門診科室' }, { CODE: '2', NAME: '住院科室' }, { CODE: '3', NAME: '其他' }],
它定義子在data里面。
這樣就發(fā)現(xiàn)錯誤了,科室的DEP_TYPE查詢出來之后如果有和Type_Ary里面的數(shù)據(jù)不一致的就會直接報錯。
二、問題解決
只需要在方法上加個判斷就行了:
const formatter_DepType = function (row, column) {
if (_this.Type_Ary && row.DEP_TYPE) {
if(typeof(_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0]) =="undefined"){
return "";
}
return _this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME;
}
return "";
}
其中:
typeof(_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0]) =="undefined"
用來判斷它是否為未定義undefined,如果未定義直接返回空,這樣就不會報錯了。
這個錯誤本質(zhì)是由_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME
引起的,如果科室類型和Type_Ary里面的不一樣就直接為未定義了,再來個.NAME,肯定直接報錯了。文章來源:http://www.zghlxwxcb.cn/news/detail-790054.html
大家的報錯可能和我的不一樣,本文僅供大家參考,當(dāng)然最有可能的錯誤還是:對象沒有數(shù)據(jù),它為undefined。文章來源地址http://www.zghlxwxcb.cn/news/detail-790054.html
到了這里,關(guān)于TypeError: Cannot read properties of undefined (reading ‘NAME‘)報錯解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!