I trying to use fetch for calls to backend from react without libs like Axios.
api.ts
export const sendSuggestion = ((data: any): Promise<any> => {
console.log(JSON.stringify(data));
const apiUrl = `/api/suggest/`;
return fetch(apiUrl, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(checkStatus)
.then(r => r.json())
.then(data => {
console.log(data);
});
});
const checkStatus = ((response: any) => {
if (response.ok) {
return response;
} else {
var error = new Error(response.statusText);
console.log(error);
//return Promise.reject(error);
}
})
Also i include npm module which is polyfill https://www.npmjs.com/package/unfetch and import it in my code
import fetch from 'unfetch'
console.log(fetch) returns in console:
function fetch() { [native code] }
I can`t understand what the problem.