最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data from express with fetch - Stack Overflow

programmeradmin5浏览0评论

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: {}
  },

  ponentWillMount: 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 ?

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: {}
  },

  ponentWillMount: 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 ?

Share Improve this question asked Oct 24, 2016 at 20:35 Kirill StasKirill Stas 1,4474 gold badges14 silver badges14 bronze badges 5
  • 1 alert needs to be in the last .then(). The data arrives asynchronously... – Kevin Commented Oct 24, 2016 at 20:39
  • I did it, but that return me undefined – Kirill Stas Commented Oct 24, 2016 at 20:46
  • 1 instead of alerting responseJson.myString try logging just responseJson and see what you get out – Endless Commented Oct 24, 2016 at 20:47
  • 1 You maybe misspelled fimlList? should it be filmList? – Endless Commented Oct 24, 2016 at 20:49
  • It is similar in server and app – Kirill Stas Commented Oct 24, 2016 at 20:57
Add a ment  | 

2 Answers 2

Reset to default 5

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 :

  ponentWillMount: function(){
    fetch('/fimlList')
    .then(function(response) {
      return response.json()
    })
    .then(function(responseJson) {
       alert(responseJson.myString);
    })
  },
发布评论

评论列表(0)

  1. 暂无评论