我剛剛了解了使用 vuex 之類的狀態(tài)管理的 vuejs。我收到了這個錯誤渲染錯誤:“TypeError:無法讀取未定義的屬性‘名稱’”當(dāng)我像這樣從服務(wù)器填充數(shù)據(jù)時:authenticated:{ created_at:"2019-12-13 16:04:47" email:"super@vetura.id" email_verified_at:"2019-12-13 16:04:47" employee:{ accountNumber:null address:"JL.VETERAN UTARA LR.124 NO.1" baseSalary:"10000000.000000" birthDate:"1987-09-14" birthPlace:"Ujung Pandang" category_id:null classification_id:null code:"000001" created_at:"2019-12-13 16:04:47" dateJoin:"2012-01-04" dateResign:null department_id:null division_id:null gender:null group_id:null height:172 id:1 idCardNumber:null kecamatan:null kelurahan:null } employee_id:1 id:1 role:"0" updated_at:"2019-12-15 14:22:26"}實際上,數(shù)據(jù)響應(yīng)可以顯示在模板上,當(dāng)我嘗試填充authenticated.employee.name控制臺時顯示錯誤但可以顯示數(shù)據(jù)。TypeError:無法讀取未定義的屬性“名稱”任何人都可以幫助我嗎?
2 回答

達令說
TA貢獻1821條經(jīng)驗 獲得超6個贊
可能是在來自服務(wù)器的響應(yīng)中,員工為空。因為用戶上沒有員工 ID,或者具有該 ID 的員工不存在。
如果以上都不是問題。就我個人而言,我喜歡使用 _.get() 來處理“路徑”中的某些內(nèi)容可能為空并引發(fā)此錯誤的情況。
let employeeName = _.get(response, 'employee.name');
這樣做是在嘗試名稱之前處理檢查員工是否不為空。如果employee 為null,則employeeName 將為null。這意味著您不必像這樣鏈接一堆父檢查:
if (response.employee && response.employee.name) {
let employeeName = response.employee.name;
}
https://lodash.com/docs/4.17.15#get
- 2 回答
- 0 關(guān)注
- 179 瀏覽
添加回答
舉報
0/150
提交
取消