简体   繁体   English

使用fetch从express获取数据

[英]Get data from express with fetch

I try to alert string which is variable in express.get and do to res. 我尝试alert在express.get中变量的字符串并执行res。 I wanna get in alert this "I am working fetch". 我想提醒这个“我正在工作”。

Here my server.js 这是我的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 我的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 ? 我做错了什么?

You assign fromServer with a promise from fetch... You try to write code as it was synchronously while in fact it's asynchronously 分配fromServer并承诺从取指...你尝试编写代码,因为它是同步,而实际上它是异步

Either move the code inside the last then function 无论是移动代码的最后内部then函数

.then(function(responseJson) {
    console.log(responseJson)
})

or use async/await to get a synchronously feeling while writing code 或者在编写代码时使用async / await来获得同步感

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 / await方法,我会建议更像这样的东西:

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);
    })
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM