API & Javascript - Pull matching data from two tables

Hello. I am trying to pull data from two tables that are linked by Product Number. Then I need to group by Product Number within a date range.

For some reason, any linked field won't show up in the results.

Here is my code:


// Show first name and note, and retrieve the last name from the connected record
$(document).on('knack-scene-render.scene_144', function (event, view, data) {

// Add a button to execute the action
$("#view_1452").prepend("<button id='showproducts'>Show Products</button>");

// Add the function to the button
document.getElementById('showproducts').addEventListener('click', function () {

$.ajax({
url: "https://api.knackhq.com/v1/objects/object_80/records",
cache: false,
async: false,
type: "GET",
headers: {"X-Knack-Application-Id": "APP ID",
"X-Knack-REST-API-Key": "API CODE"
},
success: function (product)
{

$.ajax({
url: "https://api.knackhq.com/v1/objects/object_81/records",
cache: false,
async: false,
type: "GET",
headers: {"X-Knack-Application-Id": "APP ID",
"X-Knack-REST-API-Key": "API KEY"
},
success: function (lineitems)
{
document.write("<p> *** Now matching records *** </p>");


// Loop through the product and lineitems to see if a connectionID from product matches a name ID.
// If so, print the first name from product table, the surname from lineitems table, and note from product table.
for (var x = 0; x < product.records.length; x++) {
for (var y = 0; y < lineitems.records.length; y++)
{
if (product.records[x].field_896_raw[0].id == lineitems.records[y].id)
document.write("<p>" + product.records[x].field_896 + " " + lineitems.records[y].field_908 + "</p>");
} // for y
} // for x


document.write("<p> *** DONE *** </p>");

}, // success

error: function (xhr)
{
sweetAlert("An error occured! Status: " + xhr.status + " " + xhr.statusText);
}

}); // inner ajax */


}, // outer success

error: function (xhr)
{
sweetAlert("An error occured! Status: " + xhr.status + " " + xhr.statusText);
}

}); // outer ajax

});

});

Hi,

first I don't understand why you're setting the flag async: false. This is deprecated and should not be used.

Instead, you could use the deferred object this way:

$.ajax({
..
..
}).done(function(resultx){
$.ajax({
..
..
}).done(function(resulty){
// your comparison here
});
});

Regardleess of this, I suggest to put some debugging (console.log();) here and there to see what is exactly not working.

Finally, I do not suggest you to use object-based api calls with javascript as this would expose your API key to the internetz!