后端報TypeError:Cannot?read?properties?of?null?(reading?‘xxx‘)的錯誤解決
1. 文章目錄
今天測試小姐姐,在測試某頁面時,報出如下圖的錯誤:
TypeError: Cannot read properties of null (reading 'type')
at w (http://...xxx.../assets/index.a9f96e7f.js:1052:191280)
at x (http://...xxx.../assets/index.a9f96e7f.js:952:39438)
at b (http://...xxx.../assets/index.a9f96e7f.js:952:36266)
at I (http://...xxx.../assets/index.a9f96e7f.js:986:59452)
at div
at div
at div
at div
at S (http://...xxx.../assets/index.a9f96e7f.js:1071:9994)
at x (http://...xxx.../assets/index.a9f96e7f.js:952:39438)
at b (http://...xxx.../assets/index.a9f96e7f.js:952:36266)
at I (http://...xxx.../assets/index.a9f96e7f.js:986:59452)
at w (http://...xxx.../assets/index.a9f96e7f.js:986:51920)
at r (http://...xxx.../assets/index.a9f96e7f.js:1052:16143)
at b (http://...xxx.../assets/index.a9f96e7f.js:967:8581)
at x (http://...xxx.../assets/index.a9f96e7f.js:967:10843)
at w (http://...xxx.../assets/index.a9f96e7f.js:986:66365)
at b (http://...xxx.../assets/index.a9f96e7f.js:952:36266)
at div
即TypeError: Cannot read properties of null (reading 'type')
。
2. 分析問題
正趕上最近ChatGPT
比較火,可以借助它幫助我分析問題,如下圖所示:
ChatGPT
無法回答我的問題,我只能自己分析此錯誤了。
將TypeError: Cannot read properties of null (reading 'type')
翻譯成中文,即類型錯誤:無法讀取 null 的屬性(讀取“類型”)
。
也就是說,json
存在null
值的對象。
因為,前端使用amis
框架,后端需生成amis
格式的json
對象。
前端拿到amis
格式的json
對象后,在amis
框架中渲染即可。
由于null
對象的出現(xiàn),導(dǎo)致amis
無法解析對應(yīng)的屬性。
于是,去定位出前端null
對象的位置,如下圖所示:
實際上,headerToolbar
的格式如下:
"headerToolbar": [ { "actionType": "dialog", "dialog": { "body": { "api": { "method": "post", "url": "http://xxx/common/2023030905235058401/enterprise/100/add" }, "body": [ { "name": "orgname2", "id": "u:20230309052540720", "label": "所在社區(qū)", "type": "input-text" }, ...... { "name": "ifdanger", "id": "u:20230309052540725", "label": "是否為危化企業(yè)", "type": "input-text" } ], "type": "form" }, "title": "新增" }, "level": "primary", "id": "u:20230309052540213", "label": "新增", "type": "button" }, "bulkActions" ]
如上代碼所示,正常情況下,headerToolbar
存在type
屬性。正因為上述部分代碼值為null
,導(dǎo)致amis
無法解析到type屬性
,即報出TypeError: Cannot read properties of null (reading 'type')
錯誤。
接著,再去定位到后端生成null
對象的代碼位置,如下圖所示:
因而,需要修改后端代碼。
3. 解決錯誤
根據(jù)以上分析后得知,由于后端生成的null對象
的值,導(dǎo)致amis
無法解釋后端生成的對象,即可進行如下修改:
... if (isNull(addButton)) { curdJsonVm = replace(curdJsonVm, "${headerToolbars},", ""); log.info("model page info:{}", JSONUtil.toJsonPrettyStr(curdJsonVm)); return curdJsonVm; } curdJsonVm = replace(curdJsonVm, "${headerToolbars}", JSONObject.toJSONString(addButton)); ...
重新啟動服務(wù),即可正常訪問,無報錯信息:
4. 問題總結(jié)
如果類似TypeError: Cannot read properties of null (reading ‘xxx‘)
不是后端造成的,一般是你的json
對象存在null
對象。
本來你正常的json
對象,存在某個屬性,框架能夠解析該屬性。
但出現(xiàn)了null
對象后,導(dǎo)致前端框架無法解析null
對象的屬性。
總結(jié)
到此這篇關(guān)于后端報TypeError:Cannot read properties of null (reading ‘xxx‘)的錯誤解決的文章就介紹到這了,更多相關(guān)TypeError:Cannot read properties of null內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 對接深度求索接口實現(xiàn)知識問答功能
本文詳細介紹了如何使用 Spring Boot 對接深度求索接口,實現(xiàn)知識問答功能,通過整合深度求索 API,我們可以輕松地在 Spring Boot 項目中實現(xiàn)智能問答功能,2025-02-02深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同
上次有朋友問我,java中的類可以是static嗎?我給他肯定的回答是可以的,在java中我們可以有靜態(tài)實例變量、靜態(tài)方法、靜態(tài)塊。當然類也可以是靜態(tài)的,下面小編整理了些關(guān)于java中的static class相關(guān)資料分享在腳本之家平臺供大家參考2015-11-11spring NamedContextFactory實現(xiàn)服務(wù)隔離的示例詳解
假設(shè)我們有個場景,我們需要實現(xiàn)服務(wù)之間的數(shù)據(jù)隔離、配置隔離、依賴的spring bean之間隔離,大家會有什么實現(xiàn)思路?今天給大家介紹spring-cloud-context里面有個NamedContextFactory可以達到上面的效果,需要的朋友可以參考下2024-05-05如何自定義hibernate validation注解示例代碼
Hibernate Validator 是 Bean Validation 的參考實現(xiàn) . Hibernate Validator 提供了 JSR 303 規(guī)范中所有內(nèi)置 constraint 的實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何自定義hibernate validation注解的相關(guān)資料,需要的朋友可以參考下2018-04-042024年最新IntelliJ?IDEA常用的小技巧總結(jié)(JAVA新手上路必備)
這篇文章主要介紹了2024年最新IntelliJ?IDEA常用小技巧的相關(guān)資料,文中包括IntelliJ?IDEA的概述、下載與安裝、快速創(chuàng)建并運行Java工程、詳細設(shè)置、快速開發(fā)、多模塊的IDEA工程以及最新變化,需要的朋友可以參考下2025-01-01使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細代碼
這篇文章主要介紹了使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細代碼,主要基于JWT的身份認證,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03