I'm trying to include foreign keys into my tables got from a database, one of the solutions i've come up with is to get all of the data and store them in a global variable to then use where they needed.
I've tried using a get within the current get's i have instead, though i'm sure that's not the best way of doing it as i would be getting the same data 2-3 times depending on the table called.
The error i am getting is:
"js.js:205 Uncaught TypeError: Cannot set property 'R1000002' of undefined" line 205 is in the first snippet (
"fk_RouteStations[element.ROUTE_ID] = element;"
)
params = {
headers: {
"content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*"
}
}
$.ajax({
url: "...",
type: "GET",
data: params,
success: function(result) {
result.forEach(function(element) {
fk_RouteStations[element.ROUTE_ID] = element;
})
console.log("Got ROUTE_STATIONS Data");
}
}).fail(console.log("Failed to get ROUTE_STAITONS Data"));
//foreign keys
var fk_customer;
var fk_trains;
var fk_routes;
var fk_tickets;
var fk_carriages;
var fk_RouteStations;
var fk_staff;
var fk_stations;
else if (currentTable == "ROUTES") {
var url = '...';
var id = document.getElementById("searchField").value;
const URL = url.concat(id);
const params = {
headers: {
"content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*"
}
};
console.log("Getting Routes Data");
$.ajax({
url: URL,
type: "GET",
data: params,
success: function(result) {
$("resultsDisplay").text("Got Results");
//var fk_routes = JSON.stringify(result);
var string = "<table><tr><th>ID</th><th>Price Per Stop</th><th>Route_stations</th><th>Tickets</th><th>Trains</th></tr>";
console.log("Running results")
if (id == "") {
console.log("Displaying results as array");
for (i = 0; i < result.length; i++) {
string += "<tr><td>" + result[i].ROUTE_ID + "</td><td>" + result[i].PRICEPERSTOP + "</td></tr>";
}
string += "</table>";
document.getElementById("tables").innerHTML = string;
document.getElementById("loadingPrompt").style.display = "none";
} else {
console.log("Displaying results as object")
string += "<tr><td>" + result.ROUTE_ID + "</td><td>" + result.PRICEPERSTOP + "</td><td>" + fk_RouteStations[element].ROUTE_ID + "</td><td>" + "fk_tickets[0,1]" + "</td><td>" + "TRAINS" + "</td></tr>";
string += "</table>";
document.getElementById("tables").innerHTML = string;
document.getElementById("loadingPrompt").style.display = "none";
}
makeSelectable();
}
}).fail(function() {
document.getElementById("tables").innerHTML = "User not found";
});
The expected output would be for the table to be showing the foreign keys correctly.