我尝试使用google api来获取经纬度坐标,然后将这些坐标传递给另一个根据位置查找医生的api。这两个APIS彼此独立工作,但我在使用promises将它们配置为协同工作时遇到了问题。
$(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时,它不会返回位置。
发布于 2019-03-25 05:28:24
我不确定newQuery.newDataCall(symptom, locationPromise)是如何工作的,但我认为您误用了locationPromise。假设newDataCall的第二个参数接受位置坐标,那么您的代码应该如下所示:
$(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}`);
});
})
});
});希望这能有所帮助。
https://stackoverflow.com/questions/55327925
复制相似问题