Cannot read properties of undefined (reading 'unshift')"
时间: 2023-10-15 22:22:58 浏览: 60
当你在访问一个未定义的属性时,会出现"Cannot read properties of undefined (reading 'unshift')"的错误。这个错误通常发生在你试图访问一个未定义的对象或属性时。根据引用中的描述,当你访问store.user.state.userInfo.data.id时,如果store.user.state.userInfo.data是一个空对象或undefined,那么你就会得到这个错误。为了避免这个错误,你可以在访问属性之前先检查对象是否已定义,或者使用可选链操作符?.来访问属性。 例如,你可以这样来避免错误:
```
if (store.user.state.userInfo && store.user.state.userInfo.data) {
store.user.state.userInfo.data.unshift(value);
}
```
或者使用可选链操作符?.:
```
store.user.state.userInfo?.data?.unshift(value);
```
这样,如果对象或属性未定义,就不会报错了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [解决Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘id‘)报错](https://blog.csdn.net/m0_57524265/article/details/131441971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]