I try to alert
string which is variable in express.get and do to res. I wanna get in alert this "I am working fetch".
Here my server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/publicServer'));
app.get('/fimlList', function(req, res) {
console.log('i receive a GET request');
var tryFetch = {myString: 'I am working fetch'};
res.json(tryFetch)
})
app.listen(3000);
console.log('Server running on port 3000');
my App.js
import React from 'react';
var IchBinForm = require('./IchBinForm');
var SortFilms = require('./SortFilms');
var SearchFilm = require('./SearchFilm');
var FilmShort = require('./FilmShort.js');
var FilmLong = require('./FilmLong.js');
var App = React.createClass({
getInitialState: function() {
return {
list: {}
},
componentWillMount: function(){
var fromServer = fetch('/fimlList')
.then(function(response) {
return response.json()
})
.then(function(responseJson) {
return responseJson.myString
})
alert(fromServer);
},
changeShow: function(newShow, filmId) {...},
deleteFilm: function(id) {...},
seeForChangeInForm: function(change, id) {...},
addNewFilm: function() {...},
sortMe:function() {...},
searchMe: function(searchWord) {...},
howSearch:function(whichCheckBox, val) {...},
render: function() {
....
}
}
});
return (...);
}
});
module.exports = App;
and what I get:
What do I do wrong ?
Since fetch is a web API, you will not be able to use it on server code. Although there is an npm package that can help you utilize the capabilities of fetch while writing Express code.
The developer does not have to install npm before doing networking on Node. The native Fetch API will make HTTP fetching in node environments feel much more seamless and natural. If a developer already has experience using the Fetch API, they will have no problem using the Node. js integration.
Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.
Request is fully deprecated requiring us to switch libraries (see #414 for more information).
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);
})
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With