如何在获取请求中传递变量

我正在使用React Native,并尝试理解一些示例。

我有问题

我正在尝试通过纬度进行传递,但是我遇到一个问题,谁说我的“纬度”不存在

class App extends React.Component {
constructor() {
super();
this.state = {
  region: {
    latitude: LATITUDE,longitude: LONGITUDE,latitudeDelta: LATITUDE_DELTA,longitudeDelta: LONGITUDE_DELTA,},markers: [],loaded: false
}

}

componentDidmount() {
navigator.geolocation.getcurrentPosition(
  (position) => {
    console.log(position);
    this.setState({
      region: {
        latitude: position.coords.latitude,longitude: position.coords.longitude,}
    });
  },(error) => this.setState({ error: error.message }),{ enableHighaccuracy: false,timeout: 200000,maximumAge: 1000 },);
  this.getLocations()
}


getLocations(){
  return fetch('https://***&geofilter.distance='+latitude+'%2C2.3883402698750875%2C2000') //here
  .then(response => response.json())
  .then(responseData =>{
    var markers = [];

您能帮我吗,谢谢:)

chongqingwangjing 回答:如何在获取请求中传递变量

getLocations中,您尝试使用latitude变量,但该变量中没有变量。您可能想要this.state.region.latitude

但是,如果您尝试使用this.state.region.latitude,则需要将呼叫移至getLocations中的componentDidMount,以使它在状态更改内 完成处理程序:

componentDidMount() {
  navigator.geolocation.getCurrentPosition(
    (position) => {
      console.log(position);
      this.setState({
        region: {
          latitude: position.coords.latitude,longitude: position.coords.longitude,latitudeDelta: LATITUDE_DELTA,longitudeDelta: LONGITUDE_DELTA,}
      },() => {                   // ***
        this.getLocations();    // ***
      });                       // ***
    },(error) => this.setState({ error: error.message }),{ enableHighAccuracy: false,timeout: 200000,maximumAge: 1000 },);
  // *** Not here
}
,

您正在存储纬度状态,因此您需要将其称为this.state.region.latitude

return fetch('https://***&geofilter.distance='+this.state.region.latitude+'%2C2.3883402698750875%2C2000')
,

尝试像这样使用提取:

return fetch('https://***&geofilter.distance='+this.state.region.latitude+'%2C2.3883402698750875%2C2000')
本文链接:https://www.f2er.com/3013526.html

大家都在问