1

I've been trying to retrive data from asyncstorage using react native.

Here is what i've done so far:

import React, { Component } from 'react';

import {
  Text,
} from 'react-native';

class GetData extends Component {
  constructor() {
    super()
    this.state = {
      dataLocalIds: [
        "data1Data",
        "data2Data",
        "data3Data",
        "data4Data",
        "data5Data",
        "data6Data"
      ],
    }

    this.state.dataLocalIds.map((value, index) => {
      this.data = this.getDatas(value, index);
    })
  }

  async getDatas(value, index) {
    try {
      const value = await AsyncStorage.getItem(value).then(val => {
        return JSON.parse(val)
      });
      return value
    } catch (err) {
      throw err
    }
  }

  renderScreen = () => {
      return (
        <Text> Hello World </Text>
      );
  }

  render() {
    return (
      this.renderScreen()
    );
  }
}

export default GetData;

Problem is: Apps crash with error, " the bind value at index 1 is null ".

Stacktrace does not point to lines inside my code. Instead, it points to sqlite and asyncstorage.

I really have no idea how to solve this issue. Help Would be appreciated.

4 Answers 4

2

Hi MacFlyer have you tried using multiget (https://facebook.github.io/react-native/docs/asyncstorage.html#multiget)?

Sign up to request clarification or add additional context in comments.

1 Comment

I will try. Thanks!
0

Probably its causing due to your getData() method's argument name is value and also you are declaring a new variable const value inside the try block.

when value is passed to AsyncStorage it searches for reference to immediate scope (try block) if finds a variable is declared(JS parsing and tokenizing phase) but its value is undefined. so undefined is passed to the AsyncStorage.getItem method.

Try renaming one of those.

Comments

0

try something like:

constructor(){
    super();

    this.state={...}
    this.getDataFromAsyncStorage()
}
async getDataFromAsyncStorage() {
    let dataArray = [];
    for (let value of this.state.localIds){
        let data = await AsyncStorage.getItem(value);
        dataArray.push(data)
    }
    this.data = dataArray;
}

1 Comment

also, adding checking if AsyncStorage found data or not is missing, but I believe this logic would not have the issues you're facing with your current code, so I guess it just needs some sharpening
0

1) this.myFunction = this.myFunction.bind(this) in the constructor could solve i think.

You have another problem.

Getdatas function is async and returns promise 2)

const promises = this.state.dataLocalIds.map((value, index) => this.getDatas(value, index));
const [data1, data2, ] = await Promise.all(promises)

3) the variable named value is already declared in geDatas scope

Try to fix and let us know

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.