因此,我是一位非常初级的程序员,试图了解如何使用纯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)然后答应(/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:246:38)在process._tickDomainCallback处(内部/进程/next_tick.js:229:7)
因此,我是一位非常初级的程序员,试图了解如何使用纯Javascript调用/获取Google Distance Matrix API中的数据。以下是我的代码https = require('https')函数...
我在GitHub上发现了类似的问题:https://github.com/dialogflow/dialogflow-fulfillment-nodejs/issues/22
您没有显示所有代码,但看起来getDistance()
是您已在未显示的代码中注册的Intent Handler函数。