how can i put data to ayncstorage?

Viewed 34

I want to put the token value in the maintoken through AsyncStorage. and the headers object in the mainheaders. However, when I run my code and run the getitems function, it writes to the console like this.

           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?

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>
    );
3 Answers

UserInfo.mainheaders is an object so If you want to explore it's properties, you would have to do this like

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.

NOTE: Please remove debugger after your investigation.

Metro CMD won't log object properties.

Use "Debug" mode and open Chrome console at "http://localhost:8081/debugger-ui/" (or whatever debugging tool you use)

Related