有没有一种方法可以重构和简化此服务器端代码?

时间:2018-06-21 05:34:10

标签: javascript node.js express promise knex.js

下面的代码是我的应用程序中的中间件之一,它的作用是当用户向服务器发送发布请求时,它将表单数据保存到引擎表,然后加载所有相关的引擎数据以提取平均值。将新数据插入火箭表中的相应火箭。尽管它可以工作,但是显然我的代码是如此不必要地嵌套(我认为)和冗长,因为我对promise并不熟悉。

  1. 我该如何编写更好的代码或至少从中简化一点?
  2. 注释代码行是我不明白的一件事。
  3. 谢谢。

const express = require('express');
const app = express();
const logger = require('morgan');
const knex = require('./db/index.js');
const bodyParser = require('body-parser');
const { check, validationResult, body } = require('express-validator/check')
app.use(logger(':method :url :status :date[clf]'));
app.use(express.static("public"));
app.use(bodyParser.json());



app.post('/engines/:id', (req, res) => {
    knex('engines').insert({ 
        rocket_id: req.params.id,
        user_id: 1,
        price: req.body.price,
        shining: req.body.shining,
        texture: req.body.texture,
        modern: req.body.modern,
        hardness: req.body.hardness,
        loud: req.body.loud,
        power: req.body.power,
        explosion_risk: req.body.explosion_risk
    }).then( () => {
        const rocket_temp = {};
        return rocket_temp;
    }).then( rocket_temp => {
        const knexWhere = knex('engines').where({rocket_id: req.params.id});

        knexWhere.first().count('explosion_risk').then( data => {
            rocket_temp.total_count_risk = data.count;
            return rocket_temp;
        }).then( rocket_temp => {
            knexWhere.where({explosion_risk: true}).first().count('explosion_risk').then( data => {
                rocket_temp.countTrue = data.count;
                return rocket_temp;
            }).then( rocket_temp => {
                const knexWhere = knex('engines').where({rocket_id: req.params.id}); // I don't get why knexWhere variable must be re-assigned at this point. Otherwise it won't know that it should load data from 'evaluations' table. Simply saying, the value assigned to knexWhere variable is suddenly changed from this point.(Somehow it loads data from rockets table).

                knexWhere.first(knex.raw('ROUND(AVG(price))')).then( data => {
                    rocket_temp.avg_price = data.round;
                    return rocket_temp;
                }).then( rocket_temp => {
                    knexWhere.first(knex.raw('ROUND(AVG(shining))')).then( data => {
                        rocket_temp.avg_shining = data.round;
                        return rocket_temp;
                    }).then( rocket_temp => {
                        knexWhere.first(knex.raw('ROUND(AVG(texture))')).then( data => {
                            rocket_temp.avg_texture = data.round;
                            return rocket_temp;
                        }).then( rocket_temp => {
                            knexWhere.first(knex.raw('ROUND(AVG(modern))')).then( data => {
                                rocket_temp.avg_modern = data.round;
                                return rocket_temp;
                            }).then( rocket_temp => {
                                knexWhere.first(knex.raw('ROUND(AVG(hardness))')).then( data => {
                                    rocket_temp.avg_hardness = data.round;
                                    return rocket_temp;    
                                }).then( rocket_temp => {
                                    knexWhere.first(knex.raw('ROUND(AVG(loud))')).then( data => {
                                        rocket_temp.avg_loud = data.round;
                                        return rocket_temp;    
                                    }).then( rocket_temp => {
                                        knexWhere.first(knex.raw('ROUND(AVG(power))')).then( data => {
                                            rocket_temp.avg_power = data.round;
                                            return rocket_temp;
                                        }).then( rocket_temp => {
                                            
                                            knex('rockets').where({id: req.params.id}).first().update({
                                                price: rocket_temp.avg_price,
                                                shining: rocket_temp.avg_shining,
                                                texture: rocket_temp.avg_texture,
                                                modern: rocket_temp.avg_modern,
                                                hardness: rocket_temp.avg_hardness,
                                                loud: rocket_temp.avg_loud,
                                                power: rocket_temp.avg_power,
                                                explosion_risk: Math.round(parseInt(rocket_temp.countTrue)/parseInt(rocket_temp.total_count_risk) * 100)
                                            }).then( res => {
                                                knex('rockets').select().then( res => console.log(res) )
                                            })
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })                            
    }).then( result => res.sendStatus(201) );
})

   

0 个答案:

没有答案