admin管理员组

文章数量:1022415

I'm working in a NodeJS. I want to take data from data.json, a JSON file with with coords (lat and lon) and use them to bring up the weather and other stuff, using the Open Weather API. I'm expecting to bring that coords from the JSON and then create the objects but I get my data as undefined:

Official Id: undefined
.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123

Here's a little example of the data.json:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

and here is how I'm trying to save that data in a const

function calcWeather1() {
    var data = fs.readFileSync("./json/data.json", "utf8");
    /*const data = [{
        "latjson": "33.44",
        "lonjson": "-94.04",
        "IdOficina": "1"
    },
    {
        "latjson": 2,
        "lonjson": 2,
        "IdOficina": "2"
    }
    ];*/

    for (let item of data) {
        let url = `.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=123`;

        console.log(`Official Id: ${item.IdOficina}`);
        console.log(url);
        console.log('-----------');
    }
}

The issue is that, when I send the JSON data like Randy told me:

/*const data = [{
        "latjson": "33.44",

I get:

Official Id: 1
.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid=123

it works, but when I take it from a JSON file, like:

var data = fs.readFileSync("./json/data.json", "utf8");

I get

Official Id: undefined
.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123
-----------

EDIT: I tried bringing data as Randy told me:

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);
for (let item of data) {...

and still getting undefined

EDIT: Here's my data.json, I added the way it is structured here but here is the file

I'm working in a NodeJS. I want to take data from data.json, a JSON file with with coords (lat and lon) and use them to bring up the weather and other stuff, using the Open Weather API. I'm expecting to bring that coords from the JSON and then create the objects but I get my data as undefined:

Official Id: undefined
https://api.openweathermap/data/2.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123

Here's a little example of the data.json:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

and here is how I'm trying to save that data in a const

function calcWeather1() {
    var data = fs.readFileSync("./json/data.json", "utf8");
    /*const data = [{
        "latjson": "33.44",
        "lonjson": "-94.04",
        "IdOficina": "1"
    },
    {
        "latjson": 2,
        "lonjson": 2,
        "IdOficina": "2"
    }
    ];*/

    for (let item of data) {
        let url = `https://api.openweathermap/data/2.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=123`;

        console.log(`Official Id: ${item.IdOficina}`);
        console.log(url);
        console.log('-----------');
    }
}

The issue is that, when I send the JSON data like Randy told me:

/*const data = [{
        "latjson": "33.44",

I get:

Official Id: 1
https://api.openweathermap/data/2.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid=123

it works, but when I take it from a JSON file, like:

var data = fs.readFileSync("./json/data.json", "utf8");

I get

Official Id: undefined
https://api.openweathermap/data/2.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123
-----------

EDIT: I tried bringing data as Randy told me:

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);
for (let item of data) {...

and still getting undefined

EDIT: Here's my data.json, I added the way it is structured here but here is the file

Share Improve this question edited Nov 24, 2021 at 16:48 Martín JF asked Nov 23, 2021 at 15:30 Martín JFMartín JF 1521 gold badge3 silver badges20 bronze badges 9
  • You do realize undefined is not a valid latitude right? Both lat and lon ing from your data structure are undefined. I would work on fixing that first. – Randy Casburn Commented Nov 23, 2021 at 15:48
  • @RandyCasburn it is a valid latitude as I added to the edit of my question, I forgot to say that – Martín JF Commented Nov 23, 2021 at 15:49
  • I'm simply looking at what you reported as the error: FetchError: request... clearly states both lat and lon are undefined. Unless there is a different error message that you are attempting to fix than the one you provided above, it is indisputable. – Randy Casburn Commented Nov 23, 2021 at 15:51
  • I understand you @RandyCasburn I think it is because I'm not adding the lat and lon to the constant, is that right? should I edit or create another question? – Martín JF Commented Nov 23, 2021 at 15:55
  • This should be simple to fix. This const base = ... assignment is where this happens. If you console.log(lat, long) just before that statement, you will see they are undefined there. That implies you're not parsing your data correctly. Fix that. – Randy Casburn Commented Nov 23, 2021 at 15:58
 |  Show 4 more ments

2 Answers 2

Reset to default 2

EDIT: OP is has not parsed the JSON string from the file, adding those instructions. When reading the file, you read it in as a string, it must be parsed into a JavaScript Object. Use JSON.parse() to acplish that goal.

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);

Now the data variable contains the array of objects. If you look at the first array element and ask for its jsonlat it should return the correct data:

data[0].latjson

Now you are ready to continue with the below instructions:

You are not parsing the array properly. Given the ments and the change to the data structure discussed there, this is the data to parse:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

Rather than use for...in you should really use for...of it will make it easier and will keep you out of trouble down the road with other coding you do.

Here is what that would look like:

const fakeAPIKey = 12342456478;
const data = [{
    "latjson": "33.44",
    "lonjson": "-94.04",
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
];


for (let item of data) {
  let url = `https://api.openweathermap/data/2.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=${fakeAPIKey}`;

  console.log(`Official Id: ${item.IdOficina}`);
  console.log(url);
  console.log('-----------');
}

{"cod":"400","message":"wrong latitude"} you are getting bad request response from the API. It appears from the error message that the latitude is wrong

I'm working in a NodeJS. I want to take data from data.json, a JSON file with with coords (lat and lon) and use them to bring up the weather and other stuff, using the Open Weather API. I'm expecting to bring that coords from the JSON and then create the objects but I get my data as undefined:

Official Id: undefined
.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123

Here's a little example of the data.json:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

and here is how I'm trying to save that data in a const

function calcWeather1() {
    var data = fs.readFileSync("./json/data.json", "utf8");
    /*const data = [{
        "latjson": "33.44",
        "lonjson": "-94.04",
        "IdOficina": "1"
    },
    {
        "latjson": 2,
        "lonjson": 2,
        "IdOficina": "2"
    }
    ];*/

    for (let item of data) {
        let url = `.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=123`;

        console.log(`Official Id: ${item.IdOficina}`);
        console.log(url);
        console.log('-----------');
    }
}

The issue is that, when I send the JSON data like Randy told me:

/*const data = [{
        "latjson": "33.44",

I get:

Official Id: 1
.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid=123

it works, but when I take it from a JSON file, like:

var data = fs.readFileSync("./json/data.json", "utf8");

I get

Official Id: undefined
.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123
-----------

EDIT: I tried bringing data as Randy told me:

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);
for (let item of data) {...

and still getting undefined

EDIT: Here's my data.json, I added the way it is structured here but here is the file

I'm working in a NodeJS. I want to take data from data.json, a JSON file with with coords (lat and lon) and use them to bring up the weather and other stuff, using the Open Weather API. I'm expecting to bring that coords from the JSON and then create the objects but I get my data as undefined:

Official Id: undefined
https://api.openweathermap/data/2.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123

Here's a little example of the data.json:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

and here is how I'm trying to save that data in a const

function calcWeather1() {
    var data = fs.readFileSync("./json/data.json", "utf8");
    /*const data = [{
        "latjson": "33.44",
        "lonjson": "-94.04",
        "IdOficina": "1"
    },
    {
        "latjson": 2,
        "lonjson": 2,
        "IdOficina": "2"
    }
    ];*/

    for (let item of data) {
        let url = `https://api.openweathermap/data/2.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=123`;

        console.log(`Official Id: ${item.IdOficina}`);
        console.log(url);
        console.log('-----------');
    }
}

The issue is that, when I send the JSON data like Randy told me:

/*const data = [{
        "latjson": "33.44",

I get:

Official Id: 1
https://api.openweathermap/data/2.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid=123

it works, but when I take it from a JSON file, like:

var data = fs.readFileSync("./json/data.json", "utf8");

I get

Official Id: undefined
https://api.openweathermap/data/2.5/onecall?lat=undefined&lon=undefined&exclude=hourly,daily&appid=123
-----------

EDIT: I tried bringing data as Randy told me:

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);
for (let item of data) {...

and still getting undefined

EDIT: Here's my data.json, I added the way it is structured here but here is the file

Share Improve this question edited Nov 24, 2021 at 16:48 Martín JF asked Nov 23, 2021 at 15:30 Martín JFMartín JF 1521 gold badge3 silver badges20 bronze badges 9
  • You do realize undefined is not a valid latitude right? Both lat and lon ing from your data structure are undefined. I would work on fixing that first. – Randy Casburn Commented Nov 23, 2021 at 15:48
  • @RandyCasburn it is a valid latitude as I added to the edit of my question, I forgot to say that – Martín JF Commented Nov 23, 2021 at 15:49
  • I'm simply looking at what you reported as the error: FetchError: request... clearly states both lat and lon are undefined. Unless there is a different error message that you are attempting to fix than the one you provided above, it is indisputable. – Randy Casburn Commented Nov 23, 2021 at 15:51
  • I understand you @RandyCasburn I think it is because I'm not adding the lat and lon to the constant, is that right? should I edit or create another question? – Martín JF Commented Nov 23, 2021 at 15:55
  • This should be simple to fix. This const base = ... assignment is where this happens. If you console.log(lat, long) just before that statement, you will see they are undefined there. That implies you're not parsing your data correctly. Fix that. – Randy Casburn Commented Nov 23, 2021 at 15:58
 |  Show 4 more ments

2 Answers 2

Reset to default 2

EDIT: OP is has not parsed the JSON string from the file, adding those instructions. When reading the file, you read it in as a string, it must be parsed into a JavaScript Object. Use JSON.parse() to acplish that goal.

const json = fs.readFileSync("./json/data.json", "utf8");
const data = JSON.parse(json);

Now the data variable contains the array of objects. If you look at the first array element and ask for its jsonlat it should return the correct data:

data[0].latjson

Now you are ready to continue with the below instructions:

You are not parsing the array properly. Given the ments and the change to the data structure discussed there, this is the data to parse:

[
  {
    "latjson": 1,
    "lonjson": 1,
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
]

Rather than use for...in you should really use for...of it will make it easier and will keep you out of trouble down the road with other coding you do.

Here is what that would look like:

const fakeAPIKey = 12342456478;
const data = [{
    "latjson": "33.44",
    "lonjson": "-94.04",
    "IdOficina": "1"
  },
  {
    "latjson": 2,
    "lonjson": 2,
    "IdOficina": "2"
  }
];


for (let item of data) {
  let url = `https://api.openweathermap/data/2.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=${fakeAPIKey}`;

  console.log(`Official Id: ${item.IdOficina}`);
  console.log(url);
  console.log('-----------');
}

{"cod":"400","message":"wrong latitude"} you are getting bad request response from the API. It appears from the error message that the latitude is wrong

本文标签: javascriptHow can I add data from a JSON to a constant variable in Node JSStack Overflow