简体   繁体   中英

React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined

I'm learning react-native and trying to use AsyncStorage<\/code> . I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

I'm learning react-native and trying to use AsyncStorage . I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

I'm learning react-native and trying to use AsyncStorage . I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

I was facing the same problem.

import AsyncStorage from '@react-native-async-storage/async-storage';

I'm learning react-native and trying to use AsyncStorage . I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

I'm learning react-native and trying to use AsyncStorage . I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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