Link to home
Start Free TrialLog in
Avatar of jweav1
jweav1

asked on

JQuery.get() function returning undefined

I am pulling data from twitter and need to compare the date a tweet was originated to the current date.

Twitter returns a created_at item in its JSON response which is formatted as
"Fri, 02 Jul 2010 14:03:33 +0000"  
So to make the date comparison I am pulling each date and sending it to a strtotime php function which converts it to a timestamp which can be easily compared to a timestamp of the current date.

I am sending each date individually from the JSON array because it would be to big and cumbersome to send them all attached to the URL.

This all works fine and the times are being converted.

The problem is: my convertDates function is supposed to return the converted tweetDate and load it into the converetedDates array, but the function keeps returning undefined.

I've logged the convertDates function output right before it returns and it is showing the correct timestamp output, but something is happening when it returns.

The code is attached.

Thanks in advance.




$(document).ready(function () {	
							
	 var date;
	 var convertedDates = [];
	 var theTweets;

	function convertDates(dates){ 
		$.get('convert_dates.php', {
			dates: dates
			}, function (response) {
				tweetDate = response.date;
				
				return tweetDate;
				
			}, 'json');
		
	};	
							
	jQuery.ajax({
      url: 'http://search.twitter.com/search.json?rpp=50q=happy',
	 dataType: 'jsonp',
	success: function (data) {
		theTweets = data.results;
					 
	        for (i=0; i<theTweets.length; i++){
	 
		     date = theTweets[i].created_at;
					 
		     convertedDates[i] = convertDates(date);
						
		}
					 
	       console.log(convertedDates);
					
	    }
	
	});
	 	
});



just in case here is the php for convert_dates.php:

<?php



$dates = $_GET['dates'];

function convertDates($dates){

$length = count($dates);

$newDates = array();

for ($x=0; $x<$length; $x++) {
	
	$newDates[date] = strtotime($dates[$x]);
	
}

return $newDates;

}

$result = convertDates($dates);

echo json_encode($result);

?>

Open in new window

Avatar of dirknibleck
dirknibleck

I think the problem is that your callback function isn't being called by anything other than the $.get command. As a result, returning the tweet date there isn't assigning it to anything. Try something more like:

var tDate may need to be moved into a more global context in order for this to work. I'm not 100% sure.
function convertDates(dates){ 
		
                var tDate = null;
                $.get('convert_dates.php', {
			dates: dates
			}, function (response) {
				tweetDate = response.date;			
				tDate = tweetDate;
				
			}, 'json');
		
                return tDate;
	};

Open in new window

In retrospect, you don't need the tDate variable at all, just define var tweetDate outside of your callback first.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello jweav1,

No news from you, do you need more help ?

Kind Regards