React-native:fetch的响应无法处理

时间:2017-03-12 20:42:01

标签: ajax react-native state fetch setstate

还在学习RN ...在打开网页之前,我尝试在react-native中使用fetch()来获取特定数据来自我的服务器 在智能手机的浏览器中。 这是我写的:

openLink = () => {       //Communicate to the server to get an unique key_id
  this.state = {urlKey: 'text'}; //Initial state


  var params = {
      // Some params send by POST to authenticate the request...
  };

  var formData = new FormData();

  for (var k in params) {
      formData.append(k, params[k]);
  }
      fetch(Constants.URL.root+"mobile/authorize_view", {
                method: 'POST',
                headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'multipart/form-data',
               },
                body: formData
              })
          .then((response) => response.json())
          .then((responseJson) => {
            this.setState({urlKey:responseJson.document_key}); //Getting the response, and changing the initial state (was 'text' previously)
          })
          .done();

  var urlString = Constants.URL.upload + '/' + this.state.urlKey; // !!Problem : opening in browser with this.state.urlKey = text, and not document_key!!
  Linking.canOpenURL(urlString).then(supported => {
    if (supported) {
      Linking.openURL(urlString);
    } else {
      console.log('Don\'t know how to open URI: ' + this.props.url);
    }
  });
}

实际上,正如您所看到的,我要求特定的密钥到我的服务器 urlKey ,在JSON对象中返回:responseJson.document_key)。 / p>

服务器部件中的所有内容都运行良好,因为我将此生成的document_key放在我的数据库中,我可以看到它已正确放置。

问题出在React-native部分:浏览器打开一个this.state.urlKey**text**的网页,其中初始状态,函数fetch应该是已经变成了服务器发送的document_key ... 我错过了什么?

1 个答案:

答案 0 :(得分:1)

fetch语句是异步的。这意味着当你调用fetch然后下一行执行时不需要.then但是  var urlString = Constants.URL.upload + '/' + this.state.urlKey;

在此阶段注意,如果.then未完成提取数据,则不会填充this.state.document_key。因此,为什么你会看到错误

而是在最终then中移动该代码,例如:

openLink = () => {       //Communicate to the server to get an unique key_id
  this.state = {urlKey: 'text'}; //Initial state


  var params = {
      // Some params send by POST to authenticate the request...
  };

  var formData = new FormData();

  for (var k in params) {
      formData.append(k, params[k]);
  }
      fetch(Constants.URL.root+"mobile/authorize_view", {
                method: 'POST',
                headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'multipart/form-data',
               },
                body: formData
              })
          .then((response) => response.json())
          .then((responseJson) => {
            this.setState({urlKey:responseJson.document_key}); //Getting the response, and changing the initial state (was 'text' previously)
              //moved inside then
              var urlString = Constants.URL.upload + '/' + this.state.urlKey; // !!Problem : opening in browser with this.state.urlKey = text, and not document_key!!
              Linking.canOpenURL(urlString).then(supported => {
                if (supported) {
                  Linking.openURL(urlString);
                } else {
                  console.log('Don\'t know how to open URI: ' + this.props.url);
                }
              });
          })
          .done();
}