ChrisPEditor ChrisPEditor - 4 years ago 111
Javascript Question

Having trouble getting to the JSON response after promises resolve

I'm sorry, you guys. I really hate having to ask the question; I promise, I've been through as many other questions that look even tangentially related as my patience will allow.

Following the code from the following questions:



I've got the following:

var XHR = [];

//This parses some selections on a screen, which, in turn, informs the URL inside the loop.
$("#list > input:checkbox:checked").each(function(){
result = [];
var list = $(this).val();
XHR.push($.ajax({
type:'GET',
url:"https://a-place.com/subsite/webapp/_api/web/lists/getByTitle('"+list+"')/items?$select=" + select + "&$filter=" + filter + "&$expand=" + expand + "&$top=10000",
dataType: "json",
headers: {Accept:"application/json;odata=verbose"},
complete:function(data){}
}));
});

$.when(XHR).then(function(data){
console.log(data);
});


No matter what I do, I'm only ever getting the promises inside that .when.then construction. I can't do anything with them, trying to access the responseJSON property, where all the actual objects are, does nothing. I've tried adding a return statement inside the complete callback function, that doesn't appear to change what's actually getting pushed into the XHR array.

Ideally, this is supposed to return a bunch of list items from one or more SharePoint lists that match the selected options and put the matched items into a single array that I can do stuff with.

Edit

Hmm. Ok, based on advice, I've tried:

success:function(data){}


And

success:function(data){return data.d.results;}


And nothing really changes in the console regardless of whether I use $.when.then or .done.

Thanks in advance.

Answer Source

You can try collecting all results in your success callback, and use it afterwards in 'then' function callback:

var results = [];
...
success: function(data){
    results.push(data.d.results);
}
...
$.when(XHR).then(function(){
   // Here you should have all results filled in
   console.log(results);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download