I have a backend I'm building for a class project, and I'm able to successfully post to it's DB using the following:
export function postToAPI(data) {
return fetch('http://localhost...', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrer: 'no-referrer',
body: JSON.stringify(data),
})
.then(response => {
return response.json();
})
After it posts to the DB, I want to grab that json and then push that to the Redux store. It's returning 'response.json() if it's successful, but when I console log the response it comes out as a promise, not a json object. What am I doing wrong?
I can see that the promise returns successfully in console log, but I can't figure out how to actually get the data out of it to actually post it to my redux.
I'm using a middleware that will check the action type and see if it's one to update the DB with a post action, and if so, first update the DB, get the ID from the returned JSON, and then push that info to redux, but I just can't for the life of me get the data to get out of the promise that's returned. I thought the .json() function did that, but I'm lost at this point.
I get this in response from console logging the return of that function shown above: