Thallysson Klein
Thallysson Klein

Reputation: 167

Malformed calls from JS React Native Async Storage

I came across this error today in an attempt to use AsyncStorage and was unable to resolve it.

My attempt:

import { APP_NAME } from 'react-native-dotenv';

export const createOrUpdate = async(key, content) =>{
    try{
        await AsyncStorage.setItem('@' + APP_NAME + ":" + new String(key), new String(content));
    }catch(error){
        console.log(error);
    }
}

The error: Error: Exception in HostFunction: Malformed calls from JS: field sizes are different.

does anyone have any idea how to solve?

Upvotes: 3

Views: 370

Answers (1)

rufeng
rufeng

Reputation: 121

It is recommended that you use react-native-easy-app, through which you can access AsyncStorage synchronously, and can also store and retrieve objects, strings or Boolean data

  import { XStorage } from 'react-native-easy-app';
  import { AsyncStorage } from 'react-native';
  // or import AsyncStorage from '@react-native-community/async-storage';

   export const RNStorage = {
       token: undefined, 
       isShow: undefined, 
       userInfo: undefined
   };

  const initCallback = () => {

       // From now on, you can write or read the variables in RNStorage synchronously

       // equal to [console.log(await AsyncStorage.getItem('isShow'))]
       console.log(RNStorage.isShow); 

       // equal to [ await AsyncStorage.setItem('token',TOKEN1343DN23IDD3PJ2DBF3==') ]
       RNStorage.token = 'TOKEN1343DN23IDD3PJ2DBF3=='; 

       // equal to [ await AsyncStorage.setItem('userInfo',JSON.stringify({ name:'rufeng', age:30})) ]
       RNStorage.userInfo = {name: 'rufeng', age: 30}; 
  };

  XStorage.initStorage(RNStorage, AsyncStorage, initCallback); 

Upvotes: 1

Related Questions