javascript - React-Native AsyncStorage:TypeError:无法读取未定义的属性“setItem”
问题描述
我正在学习 react-native 并尝试使用AsyncStorage
. 我正在使用此代码来存储数据:
_storeData = async (token) => {
console.log("store token");
console.log(token);
try {
await AsyncStorage.setItem('token', token);
} catch (error) {
// Error saving data
console.log(error);
}
}
调用它:this._storeData(res.token);
但它给了我这个错误:
> TypeError: Cannot read property 'setItem' of undefined
> at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
> at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
> at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
> at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
> at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
> at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
> at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
> at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
> at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
> at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)
谁能帮助我做错了什么?谢谢
解决方案
只要确保你已经import { AsyncStorage } from "react-native"
推荐阅读
- php - 如何让我的固定标题出现在正文前面?
- javascript - 如何使用下拉列表选项(Javascript)删除数组的 HTML
- c++ - 我可以在外部程序集(function.asm)中创建 C++ 方法吗?
- ios - 如何为与设备大小无关的 UICollectionView 单元格绘制网格线?
- ios - React Native - IOS 平台 - 如何在接收推送通知时从后台将应用程序置于焦点/前台
- haskell - 如何在递归函数中使用 Control.Monad.Cont?
- node.js - 如何在 Google Cloud Function 中进行异步响应?
- sql - Directus 中的自定义 SQL 查询
- c# - unity3d 方法必须有返回类型和错误 CS0501
- java - 向下滚动菜单并单击元素会单击错误的元素