如何在Node js中一对一执行多个存储过程?

时间:2018-07-14 11:57:37

标签: node.js

下面提到的代码iam用于一对一执行多个StoredProcedures。 becoz一个过程的结果取决于另一个过程。在执行所有过程并收集数据页后,将在节点js中呈现。 还有其他方法吗..如果是,请提及。

    connection = new sql.ConnectionPool(config);
    connection.connect().then(function() {
        var request = new sql.Request(connection);
        console.log("select watchlist Procedure");
        return request.input('UserId', sql.Int, req.session.userId).execute('[dbo].[UDSP_SELECT_WATCHLIST]');
    }).then(function(result){
            watchlistData=result.recordsets;
            connection.close();
            connection = new sql.ConnectionPool(config);
            connection.connect().then(function() {
            var request = new sql.Request(connection);
            console.log("Following Channel Procedure");
            console.log("Session UserId:::"+req.session.userId);
            return request.input('UserId', sql.Int, req.session.userId).execute('[dbo].[UDSP_SELECT_FOLLOWINGCHANNEL]');
        }).then(function(result){
            followingchannels=result.recordsets;
            usrfollChanId=[];
            for (var i in result.recordsets) {
                val = result.recordsets[i];
                for (var j in val) {
                    if(new String("PUBLIC").valueOf() != new String(val[j].Category).valueOf()){
                        usrfollChanId.push(val[j].ChannelId);
                    }
                }
            }
            console.log("usrfollChanId!!!"+usrfollChanId);
            connection.close();
            connection = new sql.ConnectionPool(config);
            connection.connect().then(function() {
                var request = new sql.Request(connection);
                console.log("Trending videos Procedure");
                return request.execute('[dbo].[UDSP_SELECT_TRENDINGVIDEOS]');
            }).then(function(result){
                //console.log(result);
                trendingvideosData=result.recordsets;
                connection.close();
                var videoslist=[];
                connection = new sql.ConnectionPool(config);
                connection.connect().then(function() {
                    var request = new sql.Request(connection);
                    console.log("Popular Channel Procedure");
                    return request.input('UserId', sql.Int, req.session.userId).execute('[dbo].[UDSP_SELECT_POPULARCHANNEL]');
                }).then(function (result){
                    console.log("in val");
                    //var recordCount=result.recordsets;
                    //channelVideoData = result.recordsets;
                    res.render('Home_New',{
                        recordsets:recordsets,
                        watchlistData:watchlistData,
                        followingchannelsData:followingchannels,
                        trendingvideosData:trendingvideosData,
                        popularchannelData:popularchannelData,
                        channelVideoData:result.recordsets,
                        usrfollChanId:usrfollChanId
                    });
                });
            }
        connection.close();
        });
    });

0 个答案:

没有答案