Issue with Dark Sky API

Issue with Dark Sky API
0

#1

Hi Campers,

I wonder if anyone of you had the same problem with Dark Sky API. I have been pulling my hair out, first with js code, now when my code seems to be working, with API itself :frowning:

This is my code for geolocation and AJAX request;

function myPosition(position) {
		var latitude= position.coords.latitude;
		var longitude= position.coords.longitude;
		var key= '7bb615295b47ea95df83ee01b6364297';
				
	var weatherRequest = new XMLHttpRequest();
	weatherRequest.open('GET', 'https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/' + key + '/' + latitude + ',' + longitude);
	weatherRequest.onload = function() {
	var data= JSON.parse(weatherRequest.responseText);
	console.log(data)
	
       // to console.log data shown by API //
	var d= new Date(data.currently.time);
	console.log(d);
	}

	weatherRequest.send();
	};
	
navigator.geolocation.getCurrentPosition(myPosition);

My problem is that API is showing me wrong weather. According to them there is -4 C and rainy and cloudy in my location. However there is actually over 30 C and blue sky with no clouds. Shown location is right, and I have also checked for another longitude and latitude- it shows correct location, but wrong weather. I also consoled loged the date and it shows: 18 of January 1970 around 8:00 o’clock; And it is not changing.

I have found someone else’s project done using Dark Sky API and it also shows wrong weather for my location.

Have you had same issues with Dark Sky? Maybe it is my code, or maybe my laptop , I have no idea. I would appreciate your help;

Thank you,


#2

It looks fine to me, but I’m not expert enough to be able to see errors easily yet. Here is a link to my Dark Sky API. You can check if your local weather is showing up incorrectly there if you like. It’s showing the right weather for my location. If yours is still incorrect maybe Dark Sky is drawing data incorrectly from your local weather service? You could check Dark Sky’s data sources to see where the error is coming from.

I had my weather API fail totally for a number of hours when I first completed the project and I thought I’d have to go in and re-code the whole thing. It suddenly fixed itself the next day, though. While Dark Sky is great, I don’t think it’s infallible. Maybe just wait a day and see if it fixes itself before stressing too much.

cheers,
Ali


#3

When it comes to time formating issue, I resolved it by using this libary https://momentjs.com/. Take a look especially at this section https://momentjs.com/docs/#/parsing/unix-timestamp/ as dark sky returns time as unix timestamp. You can see my project that uses dark sky here https://codepen.io/paweltar/full/VWKyXe


#4

Hi Ali,

Thanks for your response and links. I like your app, well done :slight_smile: And unfoirtunately I have the same problem with weather for my location, in your app. It is showing -18 C in London where I live. And it is boiling hot here. I have checked data source used for UK and it looks fine, and weather showed there is correct. I have no idea what is going on :frowning: but thank you for trying to help


#5

Hi Pawel,

Thanks for commenting on my post. I am impressed with your app. Nice addition with forecast for the week. And the weather for my location is more accurate- not exactly what really is but the difference aprox 5-6 C is not huge comparing to others app with dark sky api. I wonder if it might have something to do with the time formatting. I will check the library you suggested and see if it fix my app. Thanks for your help;


#6

Thanks, I’m glad you like it, in fact it was fun to make this project, however there were definitely a few frustrating “moments” - for me one of them was the date issue you mentioned, but this may be quickly fixed with the library I mentioned. The other thing is this big difference between actual and displayed temperature, I don’t think this is connected with time formatting, but who knows. It’s quite interesting. If you will be able to fix this, then share your solution here, so we will all know what may cause such differences.


#7

I am just posting update in case someone else had similar issue :slight_smile:

After couple of more hours spent on it, I gave up this project for 3 weeks. When I sat down to it again couple of days ago, I managed to fix date/ time issue. And the weather is showing correct now, but it does not look like was fixed by me or was linked to date issue. It just fixed itself on it’s own :slight_smile: Date issue was very simple to fix- I cannot believe I could not figure it out earlier. The ‘time’ property in JSON was returned in seconds and JS ‘new Date’ takes miliseconds, so just converting seconds to miliseconds resulted in correct date

my amended code for date:
var d= new Date(data.currently.time * 1000);
console.log(d);