DevGe
DevGe

Reputation: 1459

How to fix java.lang.Double cannot be cast to java.lang.String in React Native

I have problem regarding on my AsyncStorage.getItem. I have module which is the login, where I need to set the item inside the storage, after I get the result there is an yellow message below that I have Error: java.lang.Double cannot be cast to java.lang.String I didn't know where the error came.

Error

I will show you guys, my function to set the item in the storage.

     if(response.status == '200')
     {
          AsyncStorage.setItem('authorization_code',access_token);
          AsyncStorage.setItem('authorization_expiration',expires_in);
          AsyncStorage.setItem('authorization_type',token_type);

          //let token = AsyncStorage.getItem(JSON.stringify("authorization_code"));
           AsyncStorage.getItem('authorization_code', (err, result) => {
            alert(result);
          });

     }

Upvotes: 8

Views: 19758

Answers (3)

Junaid anwar
Junaid anwar

Reputation: 1

use .toString().

AsyncStorage.setItem("addressId", id.toString())
      .then((success) => {
        console.log("succesfully saved in asyncestorage");
      })
      .catch((e) => console.log(e));

Upvotes: 0

Rushan Thasindu
Rushan Thasindu

Reputation: 1

This issue comes for datatype error. once you uses a asyncstorage you must store only string data types only. if you stores a int it will pass this error. So you can use .toString() at the end of the your source code

AsyncStorage.setItem('userId', response.data.dataset[0].id.toString());

Upvotes: 0

isalgueiro
isalgueiro

Reputation: 2033

Use toString() to force type in js side. I suppose that is expires_in the floating point number here, so:

          AsyncStorage.setItem('authorization_code',access_token);
          AsyncStorage.setItem('authorization_expiration',expires_in.toString());
          AsyncStorage.setItem('authorization_type',token_type);

Upvotes: 9

Related Questions