{"id":4706,"date":"2023-01-04T17:45:58","date_gmt":"2023-01-04T09:45:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4706"},"modified":"2023-01-04T17:45:58","modified_gmt":"2023-01-04T09:45:58","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e7%9a%84typeerrorcannotreadpropertyofundefined%e9%8c%af%e8%aa%a4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e7%9a%84typeerrorcannotreadpropertyofundefined%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;TypeError:Cannotreadpropertyofundefined&#8221;\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, TypeError, Cannot read property of undefined\"><\/p>\n<h1>\u89e3\u6c7a React.js &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4<\/h1>\n<p>React.js \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u7531\u65bc React.js \u7684\u8907\u96dc\u6027\uff0c\u958b\u767c\u8005\u7d93\u5e38\u6703\u9047\u5230\u4e00\u4e9b\u554f\u984c\uff0c\u5176\u4e2d\u4e00\u500b\u6700\u5e38\u898b\u7684\u932f\u8aa4\u662f &#8220;TypeError: Cannot read property of undefined&#8221;\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u662f\u56e0\u70ba\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u5c6c\u6027\uff0c\u9019\u610f\u5473\u8457\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u5c0d\u8c61\u3002\u9019\u53ef\u80fd\u662f\u56e0\u70ba\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u5c0d\u8c61\uff0c\u6216\u8005\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u5c0d\u8c61\u7684\u5c6c\u6027\uff0c\u4f46\u8a72\u5c0d\u8c61\u672c\u8eab\u5c1a\u672a\u5b9a\u7fa9\u3002<\/p>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c0d\u8c61\u662f\u6709\u6548\u7684\uff0c\u4e26\u4e14\u8a72\u5c0d\u8c61\u5177\u6709\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c6c\u6027\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 typeof \u904b\u7b97\u7b26\u4f86\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u6709\u6548\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 hasOwnProperty \u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5177\u6709\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c6c\u6027\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u5c0d\u8c61\u7684 name \u5c6c\u6027\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u6aa2\u67e5\u8a72\u5c0d\u8c61\u662f\u5426\u6709\u6548\uff1a<\/p>\n<pre class=\"brush: javascript\">\nif (typeof object !== 'undefined' && object.hasOwnProperty('name')) {\n  \/\/ \u8b80\u53d6\u5c0d\u8c61\u7684 name \u5c6c\u6027\n}\n<\/pre>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 React.js\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u4f60\u7684\u4ee3\u78bc\u6703\u62cb\u51fa &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4\uff0c\u9019\u662f\u56e0\u70ba React.js \u5c0d\u65bc\u5c0d\u8c61\u7684\u6aa2\u67e5\u975e\u5e38\u56b4\u683c\uff0c\u56e0\u6b64\u4f60\u5fc5\u9808\u78ba\u4fdd\u4f60\u7684\u5c0d\u8c61\u662f\u6709\u6548\u7684\uff0c\u4e26\u4e14\u5177\u6709\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c6c\u6027\u3002<\/p>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0c\u8981\u89e3\u6c7a React.js &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c0d\u8c61\u662f\u6709\u6548\u7684\uff0c\u4e26\u4e14\u8a72\u5c0d\u8c61\u5177\u6709\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c6c\u6027\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 typeof \u904b\u7b97\u7b26\u548c hasOwnProperty \u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u6709\u6548\uff0c\u4e26\u4e14\u53ef\u4ee5\u78ba\u4fdd\u4f60\u7684\u4ee3\u78bc\u4e0d\u6703\u62cb\u51fa &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>React.js &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4\u662f\u4e00\u500b\u5e38\u898b\u7684\u554f\u984c\uff0c\u5b83\u662f\u7531\u65bc\u4f60\u6b63\u5728\u5617\u8a66\u8b80\u53d6\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u5c0d\u8c61\u6216\u5c6c\u6027\u6240\u5c0e\u81f4\u7684\u3002\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c0d\u8c61\u662f\u6709\u6548\u7684\uff0c\u4e26\u4e14\u8a72\u5c0d\u8c61\u5177\u6709\u4f60\u6b63\u5728\u8b80\u53d6\u7684\u5c6c\u6027\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 typeof \u904b\u7b97\u7b26\u548c hasOwnProperty \u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u6709\u6548\uff0c\u4e26\u4e14\u53ef\u4ee5\u78ba\u4fdd\u4f60\u7684\u4ee3\u78bc\u4e0d\u6703\u62cb\u51fa &#8220;TypeError: Cannot read property of undefined&#8221; \u932f\u8aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;TypeError:Cannotreadpropertyofundefined&#8221;\u932f\u8aa4\uff0c\u4e26\u77ad\u89e3\u5982\u4f55\u4f7f\u7528React.js\u7684methods()\u51fd\u6578\u4f86\u907f\u514d\u6b64\u985e\u932f\u8aa4\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-4706","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1dU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4706"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4706\/revisions"}],"predecessor-version":[{"id":4707,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4706\/revisions\/4707"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}