Get data from express with fetch
You assign fromServer
with a promise from fetch... You try to write code as it was synchronously while in fact it's asynchronously
Either move the code inside the last then
function
.then(function(responseJson) { console.log(responseJson)})
or use async/await to get a synchronously feeling while writing code
async function(){ var fromServer = await fetch('/fimlList') .then(function(response) { return response.json() }) .then(function(responseJson) { return responseJson.myString }) alert(fromServer);}
if you go by the async/await approach i would suggest something more like this:
async function(){ let response = await fetch('/fimlList') let responseJson = await response.json() let fromServer = responseJson.myString alert(fromServer)}
You're not consuming your promise , try :
componentWillMount: function(){ fetch('/fimlList') .then(function(response) { return response.json() }) .then(function(responseJson) { alert(responseJson.myString); }) },