0

仍在学习 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)。

服务器部分一切运行良好,因为我将生成的 document_key 放入我的数据库中,我可以看到它被正确放置。

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

4

1 回答 1

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();
}
于 2017-03-12T21:03:06.513 回答