在Sequelize.query执行之前,Nodejs res.status

时间:2018-03-12 15:58:12

标签: mysql node.js sequelize.js

亲爱的,我有以下服务

/* GET home page. */
router.get('/orders',function (req,res,next) {


    models.configuration.findOne( // to get the order limit and render it for ops guys
        {
            where : {

                name : "Get order limit for ops"

            }
        }).then(function(configurations) {

        models.Order.findAll({
            where : {
                userId : {
                    $and : {

                        $notIn: ['null']

                    }

                },
                serviceId : {
                    $and : {

                        $notIn: ['null']

                    }
                }
            },
            attributes : ['id','userId','providerId','orderStatus','isUsingBalance','serviceId','reason','createdAt']
            ,
            limit : parseInt(configurations.detail),
            pageLength : parseInt(configurations.detail),
            // order: 'id DESC'
            order: '`id` DESC',
            include: [
                {

                    model:  models.Provider,
                    attributes : ['id'],

                    include: [{


                        model : models.User,
                        attributes : ['firstName','lastName','phoneNumber','rankId','rate']

                    }



                    ]}

                //{model: OrderHasWarranty, as: 'warranty1'}, {model: OrderHasWarranty, as: 'warranty2'}

                    ,
                {


                    model : models.User, include :[{
                    model : models.Order,
                    attributes : ['id']
                }, {
                    model: models.Balance,
                    attributes: ['balance']
                }],
                    attributes : ['firstName','lastName','phoneNumber','id']

                }
            ]
        }).then(function (orders) {
            var starTime = moment("00:00:00", "hh:mm:ss");

            var endTime=moment("23:59:59", "hh:mm:ss");
            models.Order.count({
                where:{createdAt:{$lte: endTime,$gte: starTime}}
            }).then(function (all) {
                models.Order.count({
                    where: {orderStatus:4 ,createdAt:{$lte: endTime,$gte: starTime}}
                }).then(function (completed) {
                    models.Order.count({
                        where: {orderStatus:{$notIn: [4]},createdAt:{$lte: endTime,$gte: starTime}}
                    }).then(function (notCompleted) {
                        // get all orders having service ID = 28 which is warntty orders

                        models.Order.findAll({
                            where : {
                                userId : {
                                    $and : {

                                        $notIn: ['null']

                                    }

                                },orderStatus : {


                                        $notIn : [4]

                                },
                                serviceId : {
                                    $and : {

                                        $notIn: ['null']

                                    }
                                },
                                serviceId : 28 // means warranty orders,
                            },
                            order: '`id` DESC',
                            include: [
                                {
                                    model:  models.Provider,
                                    attributes : ['id'],

                                    include: [{


                                        model : models.User,
                                        attributes : ['firstName','lastName','phoneNumber','rankId','rate']

                                    }

                                    ]},
                                {
                                    model : models.User, include :[{
                                    model : models.Order,
                                    attributes : ['id']
                                }, {
                                    model: models.Balance,
                                    attributes: ['balance']
                                }],
                                    attributes : ['firstName','lastName','phoneNumber','id']

                                }
                            ]
                        }).then(function(warrantyOrders){


                            sequelize.query(

                                "SELECT services.`professionDecrption` AS 'Service', "+
                                "sum(if(users.`status` = 4,1,0)) AS 'Online',"+
                                "sum(if(users.`status` = 8,1,0)) AS 'Offline',"+
                                "count(providers_has_services.`serviceId`) AS 'Total' "+
                                "FROM providers, "+
                                "providers_has_services, "+
                                "users, "+
                                "services "+
                                "WHERE providers.`id` = providers_has_services.`id` "+
                                "AND users.id = providers.`userId` "+
                                "AND services.`id` = `providers_has_services`.`serviceId` "+
                                "AND services.`id` IN (1,"+
                                "2,"+
                                "3,"+
                                "4) "+
                                "GROUP BY providers_has_services.`serviceId`")
                                ,{ type: sequelize.QueryTypes.SELECT}}).then(function (providerReport,warrantyOrders) {

                                    /*

                            res.render('orders',{
                                orders  : orders,
                                status : models.Order.ORDER_STATUS,
                                service : models.Service.SERVICES,
                                ranks : models.Rank.RANKS,
                                Cancel_reason : models.Order.ORDER_CANCELLATION_REASON_AR,
                                all:all,
                                completed:completed,
                                notCompleted:notCompleted,
                                warranty_orders : warrantyOrders,
                                providerReport : providerReport
                            });

                            */




                            res.status(200).send({
                                orders  : orders,
                                status : models.Order.ORDER_STATUS,
                                service : models.Service.SERVICES,
                                ranks : models.Rank.RANKS,
                                Cancel_reason : models.Order.ORDER_CANCELLATION_REASON_AR,
                                all:all,
                                completed:completed,
                                notCompleted:notCompleted,
                                warranty_orders : warrantyOrders,
                                providerReport : providerReport,
                            });



                            });





                        })

                    })
                })
            })

        });



});

执行服务后,nodejs执行除原始查询之外的整个查询,因此nodejs返回除(providerReport)

之外的所有内容的结果

低于nodejs输出的结果

enter image description here

那么我怎么能告诉nodejs等待原始查询完成然后发送res.stats或res.render的全部细节

1 个答案:

答案 0 :(得分:0)

您的响应代码在then block中,只有在查询完成后才会执行。唯一可能发生的事情是查询可能无法返回任何结果

增强功能:您无需在原始查询中传递给 warrantyOrders ,它可以直接访问。