我定义了一些变量并使用它们来检索有效的纬度和经度,但是在我将变量传递给另一个函数后不起作用
I define some vars and i use them to retrieve the lat&lng which works, but after i pass the variable to another function doesn`t work
我遇到的问题如下
我正在使用关键字 var x =0; 在函数中创建一些变量;在某些时候,我为 x 分配了一个新值,然后将其记录到控制台,它显示了正确的值。但是当我尝试使用 x(新值)将它与 string/link 连接时,我得到 0 而不是新值。
我试图让 x 不带 0,然后说未定义。我哪里错了?
试过:
变量 x;
变量 x=0;
在第一种情况下它将是未定义的,在第二种情况下它将是 0。
geocode();
function geocode() {
var lng = 0;
var lattitude = 0;
var lattitudeb = 0;
var lngb = 0;
var locationA = '1234';
var locationB = '1323';
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: locationA,
key: 'AIzaSyBpHiHzK323fsregSMCDbgX33xAUB_Cwwi8'
}
}).then(function (response) {
lattitude = response.data.results[0].geometry.location.lat;
lng = response.data.results[0].geometry.location.lng;
console.log(lattitude);
console.log(lng);
}).catch(function (error) {
console.log(error);
});
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: locationB,
key: 'AIzaSyBpHiHzK3434fdf3434rferfeX33xAUB_Cwwi8'
}
}).then(function (response) {
lattitudeb = response.data.results[0].geometry.location.lat;
lngb = response.data.results[0].geometry.location.lng;
console.log(lattitudeb);
console.log(lngb);
}).catch(function (error) {
console.log(error);
});
axios.get('https://maps.googleapis.com/maps/api/distancematrix/json', {
params: {
units: 'imperial',
origins: lattitude + ',' + lng, // here i get always 0 instead
of the new updated value
destinations: lattitudeb + ',' + lngb,
key: 'AIzaSyBpHirfserfserfser434efddf3ISMCDbgX33xAUB_Cwwi8'
}
}).then(function (r) {
console.log(r);
}).catch(function (e) {
console.log(e);
});
}
这三个请求:
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
axios.get('https://maps.googleapis.com/maps/api/distancematrix/json', {
几乎同时发送。第三个在第一个收到纬度和其他东西之前就出去了。 lattitude
的值在那一点上仍然为零。
这就是为什么您应该:发送第一个,.then
根据其输出发送第一个。好的做法是将所需数据作为第一个承诺的 return 值传递,而不是使用外部变量。
其实这里#3依赖于#1和#2的结果,而#1和#2是独立的,我没看错吧?因此,正确的模式应该是
Promise.all([
//... first request here
//... second request here
])
.then(
//... third request here
我遇到的问题如下
我正在使用关键字 var x =0; 在函数中创建一些变量;在某些时候,我为 x 分配了一个新值,然后将其记录到控制台,它显示了正确的值。但是当我尝试使用 x(新值)将它与 string/link 连接时,我得到 0 而不是新值。
我试图让 x 不带 0,然后说未定义。我哪里错了?
试过: 变量 x; 变量 x=0;
在第一种情况下它将是未定义的,在第二种情况下它将是 0。
geocode();
function geocode() {
var lng = 0;
var lattitude = 0;
var lattitudeb = 0;
var lngb = 0;
var locationA = '1234';
var locationB = '1323';
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: locationA,
key: 'AIzaSyBpHiHzK323fsregSMCDbgX33xAUB_Cwwi8'
}
}).then(function (response) {
lattitude = response.data.results[0].geometry.location.lat;
lng = response.data.results[0].geometry.location.lng;
console.log(lattitude);
console.log(lng);
}).catch(function (error) {
console.log(error);
});
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: locationB,
key: 'AIzaSyBpHiHzK3434fdf3434rferfeX33xAUB_Cwwi8'
}
}).then(function (response) {
lattitudeb = response.data.results[0].geometry.location.lat;
lngb = response.data.results[0].geometry.location.lng;
console.log(lattitudeb);
console.log(lngb);
}).catch(function (error) {
console.log(error);
});
axios.get('https://maps.googleapis.com/maps/api/distancematrix/json', {
params: {
units: 'imperial',
origins: lattitude + ',' + lng, // here i get always 0 instead
of the new updated value
destinations: lattitudeb + ',' + lngb,
key: 'AIzaSyBpHirfserfserfser434efddf3ISMCDbgX33xAUB_Cwwi8'
}
}).then(function (r) {
console.log(r);
}).catch(function (e) {
console.log(e);
});
}
这三个请求:
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
axios.get('https://maps.googleapis.com/maps/api/distancematrix/json', {
几乎同时发送。第三个在第一个收到纬度和其他东西之前就出去了。 lattitude
的值在那一点上仍然为零。
这就是为什么您应该:发送第一个,.then
根据其输出发送第一个。好的做法是将所需数据作为第一个承诺的 return 值传递,而不是使用外部变量。
其实这里#3依赖于#1和#2的结果,而#1和#2是独立的,我没看错吧?因此,正确的模式应该是
Promise.all([
//... first request here
//... second request here
])
.then(
//... third request here