T-R*_*Rex 2 javascript arrays map promise
我有以下代码从mySQL数据库中获取信息.DB包括几个表,其中包括一个Activity表,具有channelId字段,Channel具有userId字段的User表和具有userName字段的表.该函数getAllProjectACtivities(user,project)返回一个Activity对象数组的promise ,每个对象包括所有Activity表字段.该函数findChannel(channelId)返回单个Channel对象的承诺,包括所有Channel表字段.该函数findUser(userId)返回单个User对象的承诺,包括所有User表字段.所有3个函数都会向数据库隐藏JSON Ajax请求,并且已经过测试,可以独立运行.
我在这里要做的是获取活动列表,为每个活动获取频道,为每个频道获取用户.然后我构建一个包含2个活动字段和一个用户字段的活动表.
为此,我需要一个3阶段DB访问,产生对象数组.以下是代码:
$(document).ready(function(){
var projNum=1;
var userNum=1;
var globalActivityList; //this global array will store data from the beginning of the promise chain to be used at the end of the chain
$('#tableContainer').append('<table border="0" style="background-color: lightblue;"></table>');
var table = $('#tableContainer').children();
//here goes:
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){
//alert(activity.activityChannelId);//******** shows the right stuff
findChannel(activity.activityChannelId);
}));
})
.then(function (channelList){
alert (channelList.length);//******* gives the right size
alert (channelList);//******** u-oh, this shows ,,,,,,,,,,,, - the array is empty.<<<<<<<<<<<<<<<Problem
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);
}
})
});
Run Code Online (Sandbox Code Playgroud)
会发生什么是第二个.然后得到一个没有信息的数组,当然所有后续代码都失败了.任何的想法?谢谢.
那些回调需要return一个承诺,因为它们是异步的.您的map调用当前会生成一个undefined被输入的数组Promise.all.它需要
return Promise.all(activityList.map(function(activity){
return findChannel(activity.activityChannelId);
// ^^^^^^
}));
Run Code Online (Sandbox Code Playgroud)
和
return Promise.all(channelList.map(function(channel){
return findUser(channel.channelEmployeeId);
// ^^^^^^
}));
Run Code Online (Sandbox Code Playgroud)
请注意,您globalActivityList的反模式更像是反模式.如果每个活动都会创建一个包含通道和用户的对象,那么所有这些对象的列表将成为单个承诺的结果 - 不需要离开链:
getUserProjectActivities(userNum, projNum)
.then(function(activityList) {
table.append("<tr><th>Number</th><th>Description</th><th>Employee</th></tr>");
return Promise.all(activityList.map(function(activity)
return findChannel(activity.activityChannelId);
.then(function(channel) {
return findUser(channel.channelEmployeeId);
})
.then(function(user) {
// maybe unnecessary intermediate object
return {activity:activity.activityID, description:activity.activityDescription, user:user.userName};
})
.then(function(item) {
var tableString="<tr>";
tableString+="<td>"+item.activitity+"</td>";
tableString+="<td>"+item.description+"</td>";
tableString+="<td>"+item.user+"</td>";
tableString+="</tr>";
return tableString;
});
}));
})
.then(function (list) {
for (var i=0; i<list.length; i++){
table.append(list[i]);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1167 次 |
| 最近记录: |