0

I am new to functional Javascript and promises. The code bellow works great until I uncomment this.writeDataToRealm(data.data). Then I get this error:

Possible Unhandled Promise Rejection. Cannot read property 'writeDataToRealm' of undefined

How can I send the data out to a function for further processing?

...
 fetch(url, {
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    Authorization: "Bearer " + token
  },
  }).then(function(response) {
    if (response.status !== 200) {
      throw Error(response.statusText);
    } else {
        return response.json().then(function(data) {
          console.log(data);
          return data.data;
        })
      }
    }).then(data => {
      this.writeDataToRealm(data.data)
    }, err => {
      console.log('Fetch Error: ', err);
    });

   }

   writeDataToRealm(data) {
    console.log(data[0]);
    realm.write(() => {
      realm.create('Student', {id: data[0].kp_ID, bb_first_name: data[0].kp_ID});
    });
  }
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
AdamG
  • 2,570
  • 3
  • 24
  • 35

1 Answers1

6

The unhandled rejection is because you forgot to return the inner promise from the then callback, which causes an exception not to bubble to your catch handler:

.then(function(response) {
  if (response.status !== 200) {
    console.log('Error Status Code: ' + response.status);
    // you might want to `throw` here
  } else {
    return response.json().then(function(data) {
      console.log(data);
      return data.data;
    })
  }
});

The problem with Cannot read property 'writeDataToRealm' of undefined is caused by this not being the instance you expected - see How to access the correct this / context inside a callback?. The simplest solution would be using an arrow function for the callback.

…
.then(data => {
  this.writeDataToRealm(data)
}, err => {
  console.log('Fetch Error: ', err);
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I edited my origonal code to reflect the changes you sugjested. However, I am still getting the same errors: Possible Unhandled Promise Rejection (id: 12): Cannot read property '0' of undefined TypeError: Cannot read property '0' of undefined at Database.writeDataToRealm (/app/classes/Database.map?platform=ios&runModule=false&entryModuleOnly=true&hot=true:89:23) at eval (/app/classes/Database.map? – AdamG May 12 '17 at 23:08
  • That's a different error, as now it is successfully calling the `writeDataToRealm` method. I guess I was calling it with one `.data` property access too much, though – Bergi May 13 '17 at 07:31
  • Thanks, but I am not sure what you mean by " guess I was calling it with one .data property access too much". How would I go about addressing that? – AdamG May 13 '17 at 16:02
  • @AdamG I meant [this](http://stackoverflow.com/posts/43927847/revisions). – Bergi May 13 '17 at 19:37
  • Ahhh, got it! Thanks for your help, this is now working great! – AdamG May 13 '17 at 20:00