【问题标题】:getaddrinfo ENOTFOUND API Google Cloudgetaddrinfo ENOTFOUND API 谷歌云
【发布时间】:2018-03-23 08:49:30
【问题描述】:

我正在尝试执行 API.AI 教程,为 Google 助理构建天气机器人(此处为:https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation

我成功地完成了一切,在 API 中创建了机器人,创建了 Fulfillments,在我的电脑上安装了 NodeJS,连接了 Google Cloud Platform,等等。

然后我创建了 index.js 文件,方法是使用我从世界天气组织(见下文)获得的 API 密钥完全按照 API.ai 教程中的说明复制它。

但是当我使用机器人时,它不起作用。在 Google Cloud Platform 上,错误总是一样的:

错误:getaddrinfo ENOTFOUND api.worldweatheronline.com api.worldweatheronline.com:80

    at errnoException (dns.js:28)
    at GetAddrInfoReqWrap.onlookup (dns.js:76)

无论我多久执行一次,我都会遇到同样的错误。所以我实际上并没有接触到 API。我试图查看 WWO 方面是否有任何变化(URL 等),但显然没有。我更新了 NodeJS 仍然是同样的问题。我完全刷新了 Google Cloud 平台,但没有帮助。

那个我真的无法调试。有人可以帮忙吗?

这是来自 API.ai 的代码:

'use strict';
const http = require('http');
const host = 'api.worldweatheronline.com';
const wwoApiKey = '[YOUR_API_KEY]';
exports.weatherWebhook = (req, res) => {
  // Get the city and date from the request
  let city = req.body.result.parameters['geo-city']; // city is a required param
  // Get the date for the weather forecast (if present)
  let date = '';
  if (req.body.result.parameters['date']) {
    date = req.body.result.parameters['date'];
    console.log('Date: ' + date);
  }
  // Call the weather API
  callWeatherApi(city, date).then((output) => {
    // Return the results of the weather API to Dialogflow
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
  }).catch((error) => {
    // If there is an error let the user know
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
  });
};
function callWeatherApi (city, date) {
  return new Promise((resolve, reject) => {
    // Create the path for the HTTP request to get the weather
    let path = '/premium/v1/weather.ashx?format=json&num_of_days=1' +
      '&q=' + encodeURIComponent(city) + '&key=' + wwoApiKey + '&date=' + date;
    console.log('API Request: ' + host + path);
    // Make the HTTP request to get the weather
    http.get({host: host, path: path}, (res) => {
      let body = ''; // var to store the response chunks
      res.on('data', (d) => { body += d; }); // store each response chunk
      res.on('end', () => {
        // After all the data has been received parse the JSON for desired data
        let response = JSON.parse(body);
        let forecast = response['data']['weather'][0];
        let location = response['data']['request'][0];
        let conditions = response['data']['current_condition'][0];
        let currentConditions = conditions['weatherDesc'][0]['value'];
        // Create response
        let output = `Current conditions in the ${location['type']} 
        ${location['query']} are ${currentConditions} with a projected high of
        ${forecast['maxtempC']}°C or ${forecast['maxtempF']}°F and a low of 
        ${forecast['mintempC']}°C or ${forecast['mintempF']}°F on 
        ${forecast['date']}.`;
        // Resolve the promise with the output text
        console.log(output);
        resolve(output);
      });
      res.on('error', (error) => {
        reject(error);
      });
    });
  });
} 

【问题讨论】:

    标签: api bots getaddrinfo


    【解决方案1】:

    哦,男孩,事实上,原因是有史以来最愚蠢的。我没有在 Google Cloud Platform 上启用“计费”,这就是它阻止一切的原因(即使我正在使用 API 的免费测试)。他们只是想要我的信用卡号码。现在可以使用了

    【讨论】:

    • 是的,刚刚升级到 blaze 计划(其中包括来自 spark 的免费层级数据)
    【解决方案2】:

    我在尝试访问我的数据库时遇到了同样的问题。计费不是解决办法,因为我已经启用了计费。

    对我来说,这是为 MySql 设置的 knexfile.js - 特别是 connection 对象。在该对象中,您应该将 host 键替换为 socketPath;并将 /cloudsql/ 添加到该值。这是一个例子:

    connection: {
      // host: process.env.APP_DB_HOST, // The problem
      socketPath: `/cloudsql/${process.env.APP_DB_HOST}`, // The fix
      database: process.env.APP_DB_NAME,
      user: process.env.APP_DB_USR,
      password: process.env.APP_DB_PWD
    }
    

    process.env.APP_DB_HOST 是您的实例连接名称。

    PS:我想即使您不使用Knex,在连接到 Google Cloud SQL 时,典型 DB 连接字符串的 hostserver 参数也必须称为 socketPath

    【讨论】:

      猜你喜欢
      • 2019-03-05
      相关资源
      最近更新 更多