如何在获取请求中传递变量
How can I pass variable in my fetch request
我正在使用 React Native 并尝试理解一些示例。
我有问题
我试图在我的抓取中传递纬度,但我遇到了一个问题,有人说我 "latitude" 不存在
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,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
});
},
(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 = [];
你能帮帮我吗,谢谢:)
在 getLocations
中,您尝试使用 latitude
变量,但那里的范围是 none。你可能想要 this.state.region.latitude
.
不过,如果您尝试使用 this.state.region.latitude
,则需要将呼叫移至 componentDidMount
中的 getLocations
,以便它位于 内部 [=22] =] 状态更改完成处理程序:
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')
我正在使用 React Native 并尝试理解一些示例。
我有问题
我试图在我的抓取中传递纬度,但我遇到了一个问题,有人说我 "latitude" 不存在
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,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
});
},
(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 = [];
你能帮帮我吗,谢谢:)
在 getLocations
中,您尝试使用 latitude
变量,但那里的范围是 none。你可能想要 this.state.region.latitude
.
不过,如果您尝试使用 this.state.region.latitude
,则需要将呼叫移至 componentDidMount
中的 getLocations
,以便它位于 内部 [=22] =] 状态更改完成处理程序:
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')