无法使用 Node.js 服务器连接到 Google Directions API [英] Cannot connect to Google Directions API with Node.js server

查看:29
本文介绍了无法使用 Node.js 服务器连接到 Google Directions API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过回调函数使用我的节点服务器连接到 Google Directions API,但由于某种原因,我无法从 Google API 获得响应.有趣的是,几天前我有这个工作,不知何故我设法打破了它.我的控制器包含以下代码 - 在 google_api.js 中调用一个函数:

var google = require('../scripts/google_api')无功输入 = {来源:1600 Amphitheatre Parkway, Mountain View, CA",目的地:1 Infinite Loop,Cupertino,CA 95014,美国",模式:驾驶",};函数 getDirections(数据,回调){console.log("第一个");回调(数据);console.log("第二");};getDirections(inputs, google.directions);

我的 google_api.js 文件包含以下代码(具有有效的 API 密钥):

var googleMapsClient = require('@google/maps').createClient({键:'XXX'});module.exports.directions = (req, res) =>{控制台日志(请求);googleMapsClient.directions({来源:req.origin,目的地:req.destination,模式:req.mode,},函数(错误,响应){如果(!错误){console.log(response.json.results);};});};

console.log("First")、console.log("Second") 和 console.log(req) 都按预期运行,因此问题一定存在于 googleMapsClient.directions() 函数中.API 密钥经过测试并与另一个前端 JS 函数配合使用,因此密钥没有问题.我设法让函数输出一次错误,并出现 EHOSTUNREACH 错误,但复制此错误并不一致.我担心我错过了一些非常基本的东西,我什至不知道从哪里开始.任何帮助都会很棒!谢谢

解决方案

**

<块引用>

很久以前问过,如果有人遇到并想发布一个工作代码实现这个 google api.

**

var googleMapsClient = require('@google/maps').createClient({密钥:xxxxxxxxx});函数 getDirections(请求,回调){googleMapsClient.directions({来源:req.origin,目的地:req.destination,模式:req.mode,},函数(错误,响应){控制台日志(错误);控制台日志(响应);如果(!错误){回调(response.json.results);};});};无功输入 = {来源:1600 Amphitheatre Parkway, Mountain View, CA",目的地:1 Infinite Loop,Cupertino,CA 95014,美国",模式:驾驶",};getDirections(输入,函数(结果){console.log("响应:",结果)});

I am trying to connect to the Google Directions API with my node server via a callback function but for some reason I cannot get a response from the Google API. Funny enough I had this working a few days ago and somehow I managed to break it. My controller contains the following code - calling a function within google_api.js:

var google = require('../scripts/google_api')    

var inputs = {
  origin: "1600 Amphitheatre Parkway, Mountain View, CA",
  destination: "1 Infinite Loop, Cupertino, CA 95014, USA",
  mode: "driving",
};

function getDirections(data, callback){
  console.log("First");
  callback(data);
  console.log("Second");
};
getDirections(inputs, google.directions);

My google_api.js file contains the following code (with a valid API key):

var googleMapsClient = require('@google/maps').createClient({
  key: 'XXX'
});

module.exports.directions = (req, res) => {

  console.log(req);

  googleMapsClient.directions({
  origin: req.origin,
  destination: req.destination,
  mode: req.mode,

  }, function(err, response) {
    if (!err) {
    console.log(response.json.results);
    };
  });
};

console.log("First"), console.log("Second") and console.log(req) all behave as expected so the problem must exist inside the googleMapsClient.directions() function. The API key is tested and works with another front-end JS function so it isn't a problem with the key. I managed to get the function to output an error once with a EHOSTUNREACH error but replicating this has not been consistent. I fear I am missing something so basic that I don't even know where to begin. Any help would be great! Thanks

解决方案

**

Asked long time ago, just posting a working code if anybody comes across and want to implementing this google api.

**

var googleMapsClient = require('@google/maps').createClient({
  key: xxxxxxxxxx
});

function getDirections (req, callback)  {
  googleMapsClient.directions({
  origin: req.origin,
  destination: req.destination,
  mode: req.mode,

  }, function(err, response) {
  console.log(err);
  console.log(response);
    if (!err) { 
    callback(response.json.results);
    };
  });
};

var inputs = {
  origin: "1600 Amphitheatre Parkway, Mountain View, CA",
  destination: "1 Infinite Loop, Cupertino, CA 95014, USA",
  mode: "driving",
};

getDirections(inputs, function(result){
console.log("Response: ", result)
});

这篇关于无法使用 Node.js 服务器连接到 Google Directions API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆