function v1 executes w/o error, and console log shows the expected array populated w/ the response data. However, I'm trying to make life simpler down the road by returning 2 arrays within nysQueryReturn as an object:
function v2 also executes without errors, but console logs show
nysQueryReturn {sldlBills: Array(0), slduBills: Array(0)}...empty arrays.
Function v1: works as expected
function getBillData() {
return getBills().
then(function(response) {
// save retrieved bill numbers
var billData = response;
var nysQueryReturn = [];
// get NY State Leg data for each bill number
billData.forEach(function(p) {
// Assembly bill
nysBillQuery(p.gsx$sessionyear.$t, p.gsx$assemblynum.$t).
then(function(response){
nysQueryReturn.push(response);
});
// Senate bill
nysBillQuery(p.gsx$sessionyear.$t, p.gsx$senatenum.$t).
then(function(response){
nysQueryReturn.push(response);
});
});
console.log('nysQueryReturn', nysQueryReturn);
return nysQueryReturn;
});
} // end of getBillData()
function v2: empty arrays :(
function getBillData() {
return getBills().
then(function(response) {
// save retrieved bill numbers
var billData = response;
var nysQueryReturn = {
sldlBills: [],
slduBills: []
};
// get NY State Leg data for each bill number
billData.forEach(function(p) {
// Assembly bill
nysBillQuery(p.gsx$sessionyear.$t, p.gsx$assemblynum.$t).
then(function(response){
nysQueryReturn.sldlBills.push(response);
});
// Senate bill
nysBillQuery(p.gsx$sessionyear.$t, p.gsx$senatenum.$t).
then(function(response){
nysQueryReturn.slduBills.push(response);
});
});
console.log('nysQueryReturn', nysQueryReturn);
return nysQueryReturn;
});
} // end of getBillData()
i've found several examples of "array of arrays" and "array of objects" on stackoverflow, but i can't see how to re-purpose those answers to fit my "object of arrays" scenario. any thoughts/pointers/explanations of what i'm missing would be warmly welcomed.
Thank you for your time.
edit:
k, I found this question & answer, which seems to suggest I was "doing it right". Taking another look, Chrome Dev Tools console reports that the two arrays are "empty", but when expanded they contain the expected info. Still, I can't actually access the array elements w/ nysQueryReturn.sldlBills[0].basePrintNo
without getting TypeError: Cannot read property 'basePrintNo' of undefined
, and I can't for the life of me figure out why.
What am I not getting?