简体   繁体   English

访问 JSON Covid 数据时出现问题

[英]Issue with accessing JSON Covid Data

Background on my application我的申请背景

I am using react and node js.我正在使用反应和节点 js。 I connected node.js to react.js using axios.我使用 axios 将 node.js 连接到 react.js。

Issue问题

I'm trying to access the cases (5226) in Afganistan, but I can't seem to get the right variable for it.我正在尝试访问阿富汗的案例 (5226),但我似乎无法获得正确的变量。

const cases = covidData[0].cases

Whenever, i try to run the app using nodemon (to run my server) and npm start (to run my react app) I get the following error on my terminal每当,我尝试使用 nodemon 运行应用程序(运行我的服务器)和 npm 启动(运行我的反应应用程序)我在终端上收到以下错误

Error from node terminal来自节点终端的错误

[nodemon] starting `node ./server.js`
listening at port 5000
200
undefined:1
[{"updated":1589436777722,"country":"Afghanistan","countryInfo":{"_id":4,"iso2":"AF","iso3":"AFG","lat":33,"long":65,"flag":"https://disease.sh/assets/img/flags/af.png"},"cases":5226,"todayCases":0,"deaths":132,"todayDeaths":0,"recovered":648,"active":4446,"critical":7,"casesPerOneMillion":134,"deathsPerOneMillion":3,"tests":18724,"testsPerOneMillion":481,"continent":"Asia"},{"updated":1589436777821,"country":"Albania","countryInfo":{"_id":8,"iso2":"AL","iso3":"ALB","lat":41,"long":20,"flag":"https://disease.sh/assets/img/flags/al.png"},"cases":880,"todayCases":0,"deaths":31,"todayDeaths":0,"recovered"


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (/Users/alfietorres/github/covid19/server.js:13:30)
    at IncomingMessage.emit (events.js:198:13)
    at IncomingMessage.Readable.read (_stream_readable.js:504:10)
    at flow (_stream_readable.js:973:34)
    at resume_ (_stream_readable.js:954:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
[nodemon] app crashed - waiting for file changes before starting...


JSON from website JSON 来自网站

[
 {
 "updated": 1589433177684,
 "country": "Afghanistan",
 "countryInfo": {
  "_id": 4,
  "iso2": "AF",
  "iso3": "AFG",
  "lat": 33,
  "long": 65,
  "flag": "https://disease.sh/assets/img/flags/af.png"
 },
 "cases": 5226,
 "todayCases": 0,
 "deaths": 132,
 "todayDeaths": 0,
 "recovered": 648,
 "active": 4446,
 "critical": 7,
 "casesPerOneMillion": 134,
 "deathsPerOneMillion": 3,
 "tests": 18724,
 "testsPerOneMillion": 481,
 "continent": "Asia"
 },
 {
.
.
.
etc

Node.js code Node.js码

const express = require("express");
const app = express();
const https = require("https");

const url = "https://disease.sh/v2/countries/";


app.get("/getData",function(req,res){
  https.get(url, function(response){
      console.log(response.statusCode)

      response.on("data",function(data){
        const covidData=JSON.parse(data)
        // const cases = covidData.cases
        res.send({covidData});
      })

    }, function(err){ console.log(err)})
  }
)


app.listen(5000,function(){
  console.log("listening at port 5000")
})


When you get an issue like this, the first thing to do is to check for the error, in other words add a handler for the error to see what's going wrong当你遇到这样的问题时,首先要做的是检查错误,换句话说,为错误添加一个处理程序以查看发生了什么问题

  https.get('https://disease.sh/v2/countries', function(response){
    console.log(response.statusCode)

  }, function(err){ console.log(err)})

The error is connection refused, means it cannot connect to remote server, as you can see you are missing a slash in your url, to fix it:错误是连接被拒绝,意味着它无法连接到远程服务器,您可以看到您的 url 中缺少斜杠,以修复它:

  https.get('https://disease.sh/v2/countries/', function(response){
    console.log(response.statusCode)

  }, function(err){ console.log(err)})

Or better just use the more clear version of request或者最好只使用更清晰的request版本

const request = require('request');

request('https://disease.sh/v2/countries', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', JSON.parse(body)[0].cases); // get cases from the country you specified Afghanistan
});

This is from their official docs, I just changed the url to yours.这是来自他们的官方文档,我刚刚将 url 更改为您的。

You can test this request separately by bringing up node in your terminal, just type node and then try the request without the express server.您可以通过在终端中调出节点来单独测试此请求,只需键入node ,然后在没有快速服务器的情况下尝试该请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM