zaferkrkss
zaferkrkss

Reputation: 65

Possible Unhandled Promise Rejection (id: 3): TypeError: Cannot read property 'drafts' of null TypeError: Cannot read property 'drafts' of null

I receive this error while retrieving data from storage. What is the reason? Possible Unhandled Promise Rejection (id: 3):

TypeError: Cannot read property 'drafts' of null

 @action async masterProperty(obj) {
            let observations = await AsyncStorage.getItem(AuthStore.token); //active icon receiving keyed observations
            let objObservations = JSON.parse(observations); //json observations js translating to object
            let keyNames = Object.keys(obj);
            let inValid = false;
            for (let item of objObservations.drafts) {
              if (item.ui == this.getActiveUID) {
                inValid = true;
                break;
              }
            }
            if (inValid) {
              this.updateProp(obj);
            } else {
              this.newProperty(obj);
            }
          }

Upvotes: 0

Views: 1742

Answers (1)

Gibor
Gibor

Reputation: 1721

Look at the error: Cannot read property "drafts" of null.

The only place you use something.drafts is in the for loop:

for (let item of objObservations.drafts).

This means that for some reason objObservations is null, and you get it from parsing the JSON you got from storage. Now, when Json.parse() return null - it means it was passed a "null" JSON - which means the data retrieval from storage is goign wrong.

Try check the following:

  1. check if a correct JSON object is returned from storage.

  2. check that you actually have the information in storage- because a null return might indicate that you do not.

  3. check that AsyncStorage actually uses promises and not maybe callbacks method for asynchrnousity - maybe that "await" fails because it does not use promises and than the value is not returned and observations stays empty.

Upvotes: 1

Related Questions