首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在弄清楚如何将Google地理编码器Api与另一个API结合使用时遇到了麻烦

我在弄清楚如何将Google地理编码器Api与另一个API结合使用时遇到了麻烦
EN

Stack Overflow用户
提问于 2019-03-25 03:51:45
回答 1查看 25关注 0票数 0

我尝试使用google api来获取经纬度坐标,然后将这些坐标传递给另一个根据位置查找医生的api。这两个APIS彼此独立工作,但我在使用promises将它们配置为协同工作时遇到了问题。

代码语言:javascript
复制
$(document).ready(function(){
  $("#submit").submit(function(){
    event.preventDefault();
    let newQuery = new ApiCall();
    const symptom = $("#user-input").val();
    const location = $("#user-location").val();
    let locationPromise = newQuery.locationCall(location);
    locationPromise.then(function(response){
    let locationBody = JSON.parse(response);
    },function(error){
      $('.showErrors').text(`There was an error processing your 
request: ${error.message}`);
    }).then(function(result){
    let promise = newQuery.newDataCall(symptom,locationPromise);
    promise.then(function(response){
      let body = JSON.parse(response);
      console.log(body);
      const output = parseData(body);
      const display = parseString(output);
      $(".output-field").html(display);
    }, function(error) {
      $('.showErrors').text(`There was an error processing your 
 request: ${error.message}`);
     });
   })
 });
});

我可以通过控制台记录API的两个结果,但当我尝试将位置传递给betterdoctor api时,它不会返回位置。

EN

回答 1

Stack Overflow用户

发布于 2019-03-25 05:28:24

我不确定newQuery.newDataCall(symptom, locationPromise)是如何工作的,但我认为您误用了locationPromise。假设newDataCall的第二个参数接受位置坐标,那么您的代码应该如下所示:

代码语言:javascript
复制
$(document).ready(function(){
  $("#submit").submit(function(){
    event.preventDefault();
    let newQuery = new ApiCall();
    const symptom = $("#user-input").val();
    const location = $("#user-location").val();
    let locationPromise = newQuery.locationCall(location);
    locationPromise.then(function(response){
    let locationBody = JSON.parse(response);
    // return the result of the API call
      return locationBody;
    },function(error){
      $('.showErrors').text(`There was an error processing your 
request: ${error.message}`);
    }).then(function(result){
    // result === locationBody
    let promise = newQuery.newDataCall(symptom, result);
    promise.then(function(response){
      let body = JSON.parse(response);
      console.log(body);
      const output = parseData(body);
      const display = parseString(output);
      $(".output-field").html(display);
    }, function(error) {
      $('.showErrors').text(`There was an error processing your 
 request: ${error.message}`);
     });
   })
 });
});

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55327925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档