API调用返回未定义后的流星方法



我正在使用流星,每当我对Google google google google google goole goole goole gookles and返回值时,我会得到一个不确定的值,我正在使用回调对API在那里,我不确定是什么原因

callWeather = e => {
  e.preventDefault();
  console.log(this.state.address);
  Meteor.call("geCoordinates", this.state.address, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
  });
};
geCoordinates(address) {
  googleMapsClient.geocode({ address }, (error, data) => {
    if (error) {
      console.log(error);
    } else {
      console.log(data.json.results[0].geometry.location.lat);
      return data.json.results[0].geometry.location.lat;
    }
  });
},

这是大多数人启动时犯的常见错误。这里的问题是,在您的方法代码执行回调函数之前,客户端中存在数据响应。有很多解决方案:

  1. https://stackoverflow.com/a/20090566/6880789

但是,我建议您使用Meteor.wrapAsync,如下:

let getGeoCode = Meteor.wrapAsync(googleMapsClient.geocode, googleMapsClient.geocode),
data = getGeoCode({ address });  // data contains data of your callback

最新更新