NodeJS请求:响应未定义

时间:2020-05-11 01:12:05

标签: node.js request

Res应该将json数据打印到控制台。但是返回undefined。打印错误输出“协议无效...”

findByEmail

这是我打印错误arg时遇到的错误

null

我不确定这是否重要,但是我正在鹦鹉操作系统上运行它

2 个答案:

答案 0 :(得分:1)

我建议使用Axios而不是请求,因为现在请求已过时,原因很简单。

使用axios:

const axios = require('axios');

axios
  .get(
    'http://api.weatherstack.com/current?access_key=XXXXXXXXX&query=37.8267,-122.4233'
  )
  .then((res) => console.log(res.data));

结果:

{
  request: {
    type: 'LatLon',
    query: 'Lat 37.83 and Lon -122.42',
    language: 'en',
    unit: 'm'
  },
  location: {
    name: 'North Beach',
    country: 'United States of America',
    region: 'California',
    lat: '37.806',
    lon: '-122.411',
    timezone_id: 'America/Los_Angeles',
    localtime: '2020-05-10 18:31',
    localtime_epoch: 1589135460,
    utc_offset: '-7.0'
  },
  current: {
    observation_time: '01:31 AM',
    temperature: 19,
    weather_code: 116,
    weather_icons: [
      'https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'
    ],
    weather_descriptions: [ 'Partly cloudy' ],
    wind_speed: 19,
    wind_degree: 230,
    wind_dir: 'SW',
    pressure: 1013,
    precip: 0,
    humidity: 42,
    cloudcover: 75,
    feelslike: 19,
    uv_index: 8,
    visibility: 16,
    is_day: 'yes'
  }
}

我也强烈建议在发布的代码中删除您的访问令牌。我确实在本地进行了测试,并且有效。

答案 1 :(得分:0)

存在语法错误,这就是您调用请求的方式。

快速浏览documentation

request(url, function (error, response, body) {
  console.error(error); // Print the error
  console.log(response); // Print the response object
  console.log(body); // Print the body
});

还请注意,此软件包已弃用

请求已被弃用,请参见https://github.com/request/request/issues/3142