Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"data" is required in save() #252

Open
GlimmerExands opened this issue Jul 24, 2020 · 3 comments
Open

"data" is required in save() #252

GlimmerExands opened this issue Jul 24, 2020 · 3 comments

Comments

@GlimmerExands
Copy link

-----storage.js-----
import Storage from 'react-native-storage';
import AsyncStorage from '@react-native-community/async-storage';

const storage = new Storage({
// 最大容量,默认值1000条数据循环存储
size: 1000,

// 存储引擎:对于RN使用AsyncStorage,对于web使用window.localStorage
// 如果不指定则数据只会保存在内存中,重启后即丢失
storageBackend: AsyncStorage,

// 数据过期时间,默认一整天(1000 * 3600 * 24 毫秒),设为null则永不过期
defaultExpires: null,

// 读写时在内存中缓存数据。默认启用。
enableCache: true, // 你可以在构造函数这里就写好sync的方法 // 或是在任何时候,直接对storage.sync进行赋值修改 // 或是写到另一个文件里,这里require引入

// 如果storage中没有相应数据,或数据已过期,
// 则会调用相应的sync方法,无缝返回最新数据。
// sync方法的具体说明会在后文提到
// sync: require('你可以另外写一个文件专门处理sync'),
});
global.storage = storage;

--------App.js--------------
import storage from './src/util/storage';

--------***.js-------------
storage.save({
key: 'user', // 注意:请不要在key中使用_下划线符号!
id: '1001', // 注意:请不要在id中使用_下划线符号!
data: '111111',
expires: 1000 * 60,
});
调用key-id保存方法时保存,报错信息:
[Fri Jul 24 2020 15:14:28.988] ERROR Please do not use "_" in key!
[Fri Jul 24 2020 15:14:29.800] ERROR "data" is required in save()!
[Fri Jul 24 2020 15:14:29.200] ERROR "data" is required in save()!

@sunnylqm
Copy link
Owner

感觉报错在别的地方,不是你贴出来的部分

@GlimmerExands
Copy link
Author

image
image

@sunnylqm
Copy link
Owner

sunnylqm commented Jul 24, 2020

从你给的代码片段看不太懂为什么是这个报错,目前能看到的问题是
如果你使用global,那么直接在index.js里import './src/util/storage';即可,不需要import from,后续任何地方直接使用storage

如果要模块化,那么应该export default storage,然后在每一处用到的地方import storage from './src/util/storage'

两种用法二选一

但不确定是否是导致上述报错的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants