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

为什么 storage.load() 出来的结果都是 undefined #53

Closed
henr opened this issue Nov 22, 2016 · 12 comments
Closed

为什么 storage.load() 出来的结果都是 undefined #53

henr opened this issue Nov 22, 2016 · 12 comments

Comments

@henr
Copy link

henr commented Nov 22, 2016

求助@sunnylqm
storage.save()没有抛异常,但是我调用storage.load()得到的结果都是undefined,我使用的版本是0.1.4。下面是我测试的一段代码:

storage.save({
            key: "name",
            rowData: {
                firstName: "tang",
                name: "henry"
            }
        }).then(() => {
            storage.load({
                key: "name"
            }).then(ret => {
                console.log("ret:" + ret);
            }).catch(error => {
                console.log("load error:" + error);
            })
        }).catch(error => {
            console.log("save error:" + error);
        });
@sunnylqm
Copy link
Owner

请贴完整一些的代码和控制台的输出

@henr
Copy link
Author

henr commented Nov 22, 2016

config配置:

import {AsyncStorage} from "react-native";
import Storage from "react-native-storage";

var storage = new Storage({
    //最大容量:数据条数
    size: 5000,

    //存储引擎,RN使用AsyncStorage
    storageBackend: AsyncStorage,

    //数据过期时间,单位毫秒,null表示永不过期
    defaultExpires: null,

    //读写时在内存中缓存数据。默认启用
    enableCache: true,
});

global.storage = storage;

然后我在index.android.js里面import上面的config,使用如下:

_doSave(key, value) {
        return storage.save({
            key: key,
            rowData: value
        }).then(() => {
            console.log(`save ${key} success!`);
        }).catch(error => {
            console.log(`save ${key} error:${error}`);
        });
    }
_doLoad(key) {
        return storage.load({
            key: key
        }).then(ret => {
            console.log(`load ${key} success:${ret}`);
        }).catch(error => {
            console.log(`load ${key} error:${error}`);
        });
}

先调用_doSave()再调用_doLoad()得到的ret是undefined。
@sunnylqm

@sunnylqm
Copy link
Owner

“先调用_doSave()再调用_doLoad()”
这部分代码看下

@henr
Copy link
Author

henr commented Nov 22, 2016

this._doSave("userId", "123")
            .then(() => {
                this._doLoad("userId")
                    .then(ret => {
                        console.log("result:" + ret);
                    })
                    .catch(error => {
                        console.log(error);
                    })
            })
            .catch(error => {
                console.log(error);
            })

@sunnylqm

@sunnylqm
Copy link
Owner

控制台的输出呢

@henr
Copy link
Author

henr commented Nov 22, 2016

save userId success!
load userId success:undefined
result:undefined

@henr
Copy link
Author

henr commented Nov 22, 2016

@sunnylqm 能看出我这是哪里的问题吗?

@sunnylqm
Copy link
Owner

暂时看不出来,你能打个包发到这里吗(不带node_modules)

@henr
Copy link
Author

henr commented Nov 22, 2016

StorageTest.zip

@sunnylqm

@sunnylqm
Copy link
Owner

我终于看明白了。。。你把rawData写成了rowData。。。

@henr
Copy link
Author

henr commented Nov 22, 2016

😓的确是写错了,但是我修改正确后还是undefined,你有跑我上传的sample吗? @sunnylqm

@sunnylqm
Copy link
Owner

sunnylqm commented Nov 23, 2016

我运行了是成功的
qq20161123-0

后者是undefined是因为你没有在doLoad的then中return ret

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