0

嗨,我有一个超时问题来获取 json 响应;我正在使用 google places API 来寻找最近的位置。

谁能帮我解决这个问题?谢谢。

const PlaceSearch = require("./node_modules/googleplaces/lib/NearBySearch.js");
const PlaceDetailsRequest = require("./node_modules/googleplaces/lib/PlaceDetailsRequest.js");

app.intent('Ask Location', conv => {conv.ask(new Permission({context: 'To start',permissions: 'DEVICE_PRECISE_LOCATION',}));});

app.intent('geolocation.intent', (conv,params,granted) =>{   
if(granted){    
    var coordinates = conv.device.location.coordinates;
    var location = [coordinates.latitude, coordinates.longitude];
    var searchParameters = {
        location: location,
        name:'Store Name',
        radius:10000    
    };     
    var config = {
        apiKey:'#####',
        outputFormat:'json'
    };
    var placeSearch = new PlaceSearch(config.apiKey, config.outputFormat);
    var placeDetailsRequest = new PlaceDetailsRequest(config.apiKey, config.outputFormat);     
    placeSearch(searchParameters, function (error, search_response) {
        if(search_response.status === 'OK'){             
            placeDetailsRequest({reference: search_response.results[0].reference}, function (error, details_response) {
            conv.ask(`Your closest store is at ${details_response.result.formatted_address}.`);
            });            
        }
    });
}
});
4

1 回答 1

1

我通过 URL 使用对 Google API 的请求解决了这个问题;并使用承诺。

const request = require("request");
app.input("geolocation.intent", conv => {
  return new Promise((resolve, reject) => {
    ...
    request(options, (error, response, body) => {
      ...
      if (error) {
        ...
        reject(...);
      } else {
        ...
        resolve(...);
      }
  }).then(result => {
    const address = result.address;
    conv.ask('Your closest store is...');
  }).catch(error => {
    conv.close('Error in Promise');
  });
});

我了解到的是,在 Dialogflow API v2 中,您需要在发出请求时使用 Promise。

于 2018-11-08T16:59:15.907 回答