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