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:
Pos
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);
});