Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTadjiev Codes
9,626 PointsAJAX Nasa Apod API
Dear Treehouse Folks,
If anyone pls could help to understand how to work with this small project Apod API.
First, I need to select dates within from Date: and To Date boxes, and then when I click Get Data Button display JSON data between those dates. Now it only shows what's selected for one day from the second Date Box.
And, I shouldn't allow users to select more than the 1-month worth of data.
Secondly, I should make Ajax calls only when the “From/To” date selection changes, if dates have not changed I should not make another Ajax call to get the same data.
Third of all, If any property is missing from your AJAX call, (no data was returned) use the word “Unknown” instead of allowing display “undefined” or similar. I watched some courses on how to do it but I can't remember.
//The workspace link: https://w.trhou.se/95tjzrkpn3
Mr.Zimri, If you could take a look at these problems, please🙏🙏🙏 Zimri Leijen Last time you helped me out with the AJAX problem.
7 Answers
Steven Parker
231,269 PointsThe value of the date picker is a string. You probably want to convert it to an actual datetime object before doing any comparisons or calculations:
let date_val = calandar.value; // existing code
let actual_date = new Date(date_val); // convert string to object
Tadjiev Codes
9,626 PointsDear Mr.Steven Steven Parker🙏🙏🙏 Could you please take a look at these problems and advise on what to do to fix them whenever u have time?
Especially on how to get the values from two date pickers from the range of those two dates selected. I can't get my head around that. Thanks a lot)))
I didn't want to bother you this time but seems like you're the only one who could help out here mostly)
Tadjiev Codes
9,626 PointsOR could this Jquery function help me out with date selection in two date pickers?
input class="form-control" id="txtFromDate" />
<input class="form-control" id="txtToDate" />
$(document).ready(function () {
$("#txtFromDate").datepicker({
format: 'dd/mm/yyyy',
autoclose: 1,
//startDate: new Date(),
todayHighlight: false,
//endDate: new Date()
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#txtToDate').datepicker('setStartDate', minDate);
$("#txtToDate").val($("#txtFromDate").val());
$(this).datepicker('hide');
});
$("#txtToDate").datepicker({
format: 'dd/mm/yyyy',
todayHighlight: true,
//endDate: new Date()
}).on('changeDate', function (selected) {
$(this).datepicker('hide');
});
});
Tadjiev Codes
9,626 PointsThanks Mr.Steven. But when I turn it into Date Object and do some validations. It throws a bunch of errors.
Also, the Fetch method sometimes very rarely gives errors of 500 maybe cuz of the ad blocker. I dunno but most of the time it works fine. SO is it better to use XHR request the old way of AJAX to check specifically for errors like if readystatechange = 200 and other checks?
The new workspace : https://w.trhou.se/8gtrn0olm6
On Github: https://github.com/TadjievCodes/Nasa_Project_Another_Version
I added some styling if you could please review if the fetch API works fine or it's better if I used the old style that I learned first.
And I can't seem to understand how to connect these two datepickers and get the range of values.
// Second on the right date picker
date_btn.addEventListener('click', function(event) {
let date_val = calendar.value;
//And Do I really need to convert it here to Date Object as it throws errors when I do it that way?
let actual_date = new Date(date_val); // convert string to Date Object
// preventing default behaviour
event.preventDefault()
if (date_val < min_date || date_val > new Date()) {
console.log("out of range!!")
} else {
getData(date_val).catch(err => {
console.log(err)
});
}
})
// First Date Picker
date_btn.addEventListener('click', function(event) {
let date_val = calendar1.value;
let actual_date = new Date(date_val); // convert string to Date Object
// preventing default behaviour
event.preventDefault()
if (date_val < min_date || date_val > new Date()) {
console.log("out of range!!")
} else {
getData(date_val).catch(err => {
console.log(err)
});
}
})
Tadjiev Codes
9,626 PointsOkay, now I see what you meant.
// Second on the right date picker
date_btn.addEventListener('click', function(event) {
let date_val = calendar.value;
let actual_date = new Date(date_val); // convert string to Date Object
// preventing default behaviour
event.preventDefault()
if (actual_date < min_date || actual_date > new Date()) {
console.log("out of range!!")
} else {
getData(date_val).catch(err => {
console.log(err)
});
}
})
I was converting the else part to actual_date as well and that was giving an error.
Tadjiev Codes
9,626 PointsBut what way would connect these two date pickers so it returns the range of values? I saw bunch of examples with Jquery Code on StackOverflow but I can't make them work even though I add Jquery script external to connect Jquery. I don't understand jquery at all. Also, it might be the reason.
Tadjiev Codes
9,626 PointsI've just created another question for the other part and selected the best answer here. Thanks a lot) The link: https://teamtreehouse.com/community/fetch-api-nasa-apod-api-date-pickers-issue
Steven Parker
231,269 PointsSteven Parker
231,269 PointsNote that after creating the "actual_date", you need to use it instead of "date_val" in the comparisons:
if (actual_date < min_date || actual_date > new Date()) {