Avatar of Isaac
Isaac
Flag for United States of America asked on

REST API - 'results' undefined

I have a list and when the page loads I want to grab the items from my list and add it to an array. I am getting an error when I try to read the rest data.  Here's what i have so far.

<script>
$(document).ready(function() { 
	var iceCreamApp = {};
	iceCreamApp.Truck = new Array();
	
	//Load data from list into array right away
	(function() {
		var url = "http://isaac.issharepoint.com/demo/_vti_bin/listdata.svc/IceCreamTrucks";
		$.ajax({
			url:url,
			method:"GET",
			headers: {"Accept": "application/json; odata=verbose"},
			success: function(data) {
				success(data.d);
			},
			error: function(data) {
				failure(data.responseJSON.error);
			}
		})
			
			function success(data)
			{
				console.log(data.d.results);
			}
	}());
	
	
})</script>

Open in new window


Here's the error I get
SCRIPT5007: Unable to get property 'results' of undefined or null reference

Points to this line
console.log(data.d.results);
Any help would be great. thanks
Web ServicesMicrosoft SharePointJavaScript

Avatar of undefined
Last Comment
Isaac

8/22/2022 - Mon
Jamie McAllister

It's a little typo I think;

You call Success like this;

success(data.d);

But the signature is this;

function success(data)

So you're effectively calling console.log(data.d.d.results); in your code, hence the null ref.
Isaac

ASKER
Thanks but now I get a blank page.  None of the data is rendering in the console.
Here's the updated code:

(function() {
		var url = "http://isaac.issharepoint.com/demo/_api/web/lists/GetByTitle('IceCreamTrucks')/items?$select=State";
		$.ajax({
			url:url,
			type:"GET",
			dataType:"json",
			headers: {"Accept": "application/json; odata=verbose"},
			success: function(data) {
				success(data);
			},
			error: function(data) {
				//failure(data);
			}
		})
			
			function success(data)
			{
				console.log(data);
			}
	}());

Open in new window

ASKER CERTIFIED SOLUTION
Jamie McAllister

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Isaac

ASKER
When I had the failure line uncommented, that's what rendered. I will try data.d
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Isaac

ASKER
That still does not work for me.  Here's the page below.
http://isaac.issharepoint.com/demo/SitePages/iceCream.aspx