所以我是一个非常初级的程序员,试图了解如何在纯 Javascript 中从 Google Distance Matrix API 调用/获取数据。
下面是我的代码
https = require('https')
function getDistance(app){
console.log('testing1');
let defaultPath = 'maps.googleapis.com/maps/api/distancematrix/json?units=metric';
let APIKey = '&key=<API KEY HERE>';
let origin = 'Tampines Avenue 1, Temasek Polytechnic';
let destination = 'Tampines Central 5, Tampines Mall';
let newPath = defaultPath+ '&origins=' + origin + '&destinations=' +destination + APIKey;
console.log('https://'+ newPath); //this prints the correct path
https.get('https://'+ newPath, (res) =>{ //i assume the problem begins here?
console.log('testing2')//doesnt print
let body = ''; //no idea what this does. Copied from a school lab sheet
res.on('data', (d) => {
console.log('testing3') //this doesn't print
let response = JSON.parse(body);
let distance = response.rows[0].elements.distance.text //are these correct?
let travelTime = response.rows[0].elements.duration.text
console.log(distance) //doesnt print
console.log(response.rows[0]) //doesnt print
app.add('distance between is ' + distance + '. Time taken is ' + travelTime);
console.log(response);
});
});
}
我很确定 'https://'+ newPath 是正确的,因为它打印在 console.log 中
并将链接扔到浏览器中,我得到了这个结果
所以有人可以向我解释我做错了什么吗?哦,还有,不知道这是否有必要,但我在 dialogflow.cloud.google.com 中这样做作为我任务的聊天机器人
这是我得到的错误
错误:没有为平台定义响应:未定义在 WebhookClient.send_ (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:428:13) at promise.then (/srv/node_modules/dialogflow-fulfillment/src /dialogflow-fulfillment.js:246:38) 在 process._tickDomainCallback (internal/process/next_tick.js:229:7)
哆啦的时光机
慕桂英4014372
相关分类