代码之家  ›  专栏  ›  技术社区  ›  HUNG

Knex后的回调函数不工作

  •  0
  • HUNG  · 技术社区  · 7 年前

    从下面的代码中,我想选择一些数据,然后在 适应条件,最后运行两个URL来执行其他一些任务。

    我可以更新第二个Knex更新查询,但是它没有运行两个post请求中的任何一个,我想知道为什么?

    router
        .get('/xxxx', function(req, res){
            _DB_Knex('xxx')
            .where({
                "xxxx": "xxxx"
            })
            .select('xxxx.*', 'xxx.xxx as xxx', 'xxx.xxxx')
            .leftJoin('xxxx', 'xxx.xxx', 'xxx.xxx')
            .then (function (data) {
    
            if(data && data.length>0){
                for(var i=0; i<data.length; i++){
                    if(xxxxx){
                        var xxx = xxxxx;
                        var xxx = data[i].xxxx;
                        var xxx = data[i].xxxx;
                        var xxx = data[i].xxx;
    
                        if(xxx>=xxx){
                            _DB_Knex('xxxx')
                            .where({
                               xxx: "xxxx",
                               xxx: xxxx                                
                             })
                             .update({
                               xxxx : "xxxx"
                             })
                             .then(function(){
                                 request.post({
                                    url: `${api_url}/xxxxx/s`,
                                      body: { 
                                        xxx: xxxx
                                      },
                                      json: true
                                 });
    
                                 request.post({
                                     url: `${api_url}/xxxx/xxxx`,
                                      body: { 
                                         xxx: xxxx
                                      },
                                     json: true
                                 });
    
                                return null;
                           });
                       }
                  }
              }
            }
      }});
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   danh    7 年前

    发布的代码是经过编辑的,所以很难精确地显示如何重构它,但是有一个必需的更改和一个建议的更改。

    建议的更改是 router.get() . 先看看那个…

    // get the xxx route.  return a promse that resolves to the get response
    // TODO: error handling
    function getXXX() {
        return new Promise(function(resolve, reject) {
            router.get('/xxxx', function(req, res){
                resolve(res);
            });
        });
    }
    

    叫…

    function theOPFunction() {
        return getXXX().then(function(res) {
            return _DB_Knex('xxx')
                .where({ "xxxx": "xxxx" })
                .select('xxxx.*', 'xxx.xxx as xxx', 'xxx.xxxx')
                .leftJoin('xxxx', 'xxx.xxx', 'xxx.xxx')
        }).then(function(data) {
            return loopAndGatherPromises(data);  // see below
        })
    }
    

    所需的变化是 data 必须收集其中产生的承诺,并用 Promise.all() .

    function loopAndGatherPromises(data) {
        let promises = [];
        if(data && data.length>0){
            for(var i=0; i<data.length; i++){
                if(xxxxx){
                    var xxx = xxxxx;
                    var xxx = data[i].xxxx;
                    var xxx = data[i].xxxx;
                    var xxx = data[i].xxx;
    
                    if(xxx>=xxx){
                        promises.push(updateAndPost(data));
                    }
                }
            }
        }
        return Promise.all(promises);
    }
    
    
    // updateAndPost answers a chain of three promises update the db and
    // post to two web services. note these 3 chained promises probably
    // could be made parallel with promise.all
    function updateAndPost(data) {
        return _DB_Knex('xxxx').where({xxx: "xxxx",xxx: xxxx}).update({ xxxx : "xxxx"}).then(function(){
            return request.post({
                url: `${api_url}/xxxxx/s`,
                body: { xxx: xxxx },
                json: true
            });
        }).then(function() {
            return request.post({
                url: `${api_url}/xxxxx/xxxx`,
                body: { xxx: xxxx },
                json: true
            });
        });
    }
    
    推荐文章