我已经尝试从下面的代码中获得响应:
let latlongdata;
if (userInfo.address != "" && userInfo.country != "") {
let add = userInfo.address + "," + userInfo.country;
geo.find(add, function(err, res) {
latlongdata = res[0].location.lat;
console.log("Inside output", res[0].location.lat); // Got response
});
console.log("Outside output", getletlong); // No response
}
}在“内部输出”上我得到了响应,在“外部输出”上我没有得到响应。
我也试过使用async await,下面是代码:
if (userInfo.address != "" && userInfo.country != "") {
add = userInfo.address + "," + userInfo.country;
[err, data] = await to(geo.find(add));
console.log("output", data);
console.log("error", err);
} 最后,我在使用setTimeOut()时得到了响应
let latlongdata;
if (userInfo.address != "" && userInfo.country != "") {
add = userInfo.address + "," + userInfo.country;
geo.find(add, function(err, res) {
if (res) {
userInfo.latitude = res[0].location.lat;
}
});
}
userInfo.role_id = 2;
setTimeout(async () => {
[err, user] = await to(User.create(userInfo));
}, 300);但我认为这不是正确的方法,而且我认为这不会在所有情况下都适用,请任何人告诉我在前两种方法中我做错了什么。
我使用.:- "google-geocoder":"^0.2.1“
发布于 2019-09-26 20:19:56
我做了一些研究和开发,找到了下面的解决方案
let add = userInfo.address + "," + userInfo.country;
return new Promise((resolve, reject) => {
geo.find(
"India",
(err, res) => {
if (res) {
resolve(res);
}
if (err) {
reject(err);
}
},
err => {
reject(err);
}
);
});https://stackoverflow.com/questions/58115747
复制相似问题