简体   繁体   English

如何将数据放入 ayncStorage?

[英]how can i put data to ayncstorage?

I want to put the token value in the maintoken through AsyncStorage.我想通过 AsyncStorage 将令牌值放入 maintoken 中。 and the headers object in the mainheaders.以及主标题中的标题 object。 However, when I run my code and run the getitems function, it writes to the console like this.但是,当我运行我的代码并运行 getitems function 时,它会像这样写入控制台。

           maintoken : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjE2OTU5OTQ1NzIiLCJwcm92aWRlciI6Imtha2FvIiwiaWF0IjoxNjE4NTYyNzYyfQ.qnbFrRKxwL-9JK9O1aarTUvUYWnqU9pUNl2aO4IqGuc

          mainheaders : [object Object]

The token value comes out well, but the mainheaders come out like this.令牌值很好,但主标题是这样出来的。 How do I make the object come out?如何让 object 出来?

this is my code这是我的代码

    useEffect(() => {
      Linking.addEventListener('url', async ({url}) => {
        AsyncStorage.setItem(
          'tokenstore',
          JSON.stringify({
            maintoken: `${token}`,
            mainheaders: {
              headers: {Authorization: `Bearer ${token}`},
            },
          }),
          () => {
            console.log('success');
          },
        );
      });
      return () => Linking.removeEventListener('url');
    }, []);

    const getitems = () => {
      AsyncStorage.getItem('tokenstore', (err, result) => {
        const UserInfo = JSON.parse(result);
        console.log('maintoken : ' + UserInfo.maintoken); //
        console.log('mainheaders :' + UserInfo.mainheaders); //
      });
    };

    return (
      <LoginButton onPress={() => getitems()}>
        <Label>getItem</Label>
      </LoginButton>
    );

UserInfo.mainheaders is an object so If you want to explore it's properties, you would have to do this like UserInfo.mainheaders是一个 object 所以如果你想探索它的属性,你必须这样做

console.log('mainheaders :' + JSON.stringify( UserInfo.mainheaders ));
console.log('maintoken : ' + UserInfo.maintoken); //
console.log('mainheaders :' + UserInfo.mainheaders); //
debugger;

keep debugger;保留调试器; (by opening debugger tools) before or after and check the object values based on that use JSON stringy verify and utilize the same. (通过打开调试器工具)之前或之后检查 object 值,基于使用 JSON 字符串验证和使用相同。

NOTE: Please remove debugger after your investigation.注意:请在调查后删除调试器。

Metro CMD won't log object properties. Metro CMD 不会记录 object 属性。

Use "Debug" mode and open Chrome console at "http://localhost:8081/debugger-ui/" (or whatever debugging tool you use)使用“调试”模式并在"http://localhost:8081/debugger-ui/" (或您使用的任何调试工具)打开 Chrome 控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM