1

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.org/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.org/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.org/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.org/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

9
  • You do realize undefined is not a valid latitude right? Both lat and lon coming from your data structure are undefined. I would work on fixing that first. 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 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. 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? 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. Commented Nov 23, 2021 at 15:58

2 Answers 2

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 accomplish 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 comments 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.org/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('-----------');
}

Sign up to request clarification or add additional context in comments.

17 Comments

I got another error lol, but this answers the other, thank you!
mate, should I change it to const lat = item.latjson; ?
yes - that would be it!
Mate I apologise for accepting the answer, but I get Successful connection [] Official Id: undefined Latitude: undefined Longitude: undefined ----------- do you know what can be? do I need to unaccept it? I dont know how stackoveflow works in that things
I've updated my answer to show exactly how to accomplish the goal. It should be very close to what you are attempting to accomplish.
|
0

{"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

1 Comment

I edited my question, I would really apreciate if you could check it out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.