My current issue involves retrieving data from a mySQL DB by using the following code. Within the database, there are multiple tables including an Activity
table with a channelId field, a Channel
table with a userId field, and a User
table with a userName field.
The function
getAllProjectACtivities(user, project)
is responsible for returning a promise containing an array of Activity
objects, each encompassing all fields from the Activity
table.
Additionally, the function findChannel(channelId)
returns a promise containing a single Channel
object with all associated fields, while the function findUser(userId)
provides a promise for a single User
object along with its respective table fields.
These functions perform JSON Ajax requests to the database and have been individually verified as functioning correctly.
My objective is to retrieve a list of activities, obtain the channel for each activity, and then identify the user for each channel. Subsequently, I aim to construct a table consisting of two activity fields and one user field.
To achieve this, a three-phase process of DB access is required, yielding arrays of objects. The provided code snippet outlines this process:
$(document).ready(function(){
var projNum=1;
var userNum=1;
var globalActivityList;
$('#tableContainer').append('<table border="0" style="background-color: lightblue;"></table>');
var table = $('#tableContainer').children();
getUserProjectActivities(userNum, projNum)
.then(function (activityList){
table.append("<tr><th>Number</th><th>Description</th><th>Employee</th></tr>");
globalActivityList = activityList;
return Promise.all(activityList.map(function(activity){
findChannel(activity.activityChannelId);
}));
})
.then(function (channelList){
alert(channelList.length);
alert(channelList);
return Promise.all(channelList.map(function(channel){
findUser(channel.channelEmployeeId);
}));
})
.then(function(userList){
for (var i=0; i<userList.length; i++){
var tableString="<tr>";
tableString+="<td>"+globalActivityList[i].activitityId+"</td>";
tableString+="<td>"+globalActivityList[i].activitityDescription+"</td>";
tableString+="<td>"+userList[i].userName+"</td>";
tableString+="</tr>";
table.append(tableString);
}
})
});
The challenge arises when the second .then
receives an array without any information, causing subsequent code execution failures. Any insights on how to resolve this would be greatly appreciated. Thank you.