NodeJ如何在对象中创建动态值

时间:2019-11-28 05:33:57

标签: node.js

我希望它能够动态变化多次。 我该怎么办??

wherequery = {
  [columnname[1]] : {
      [Op.like]: '%'+columnvalue[1] +'%'
    },
    [columnname[2]] : {
      [Op.like]: '%'+columnvalue[2] +'%'
    },
    [columnname[3]] : {
      [Op.like]: '%'+columnvalue[3] +'%'
    }
};

exports.searchAll = (req,res,next) =>{

 var columnname =  Object.keys(req.body);
 var columnvalue =  Object.values(req.body);

// var count = Object.keys(req.body).length;

var wherequery =[];

console.log(req.body);
// console.log(wherequery);
var includeRelation=[];
    if (req.body.tableName =='applications' || req.body.tableName =='producers' || req.body.tableName == 'recyclers' ){

      //For Closed Producers
if (columnname.includes('producerId')){
  console.log("In closed producer");
  wherequery = {
    status: 1, [columnname[4]]:[columnvalue[4]] ,
    [columnname[1]] : {
      [Op.like]: '%'+columnvalue[1] +'%'
    },
    [columnname[2]] : {
      [Op.like]: '%'+columnvalue[2] +'%'
    },'$recycler.name$':{
          [Op.like]: '%'+columnvalue[3]+'%'
        }
      }
     includeRelation =   [{
        model: db.producers,
        required: true,
        as: 'producer',
        all: true
      },
      {
        model: db.recyclers,
        required: true,
        as: 'recycler',
        all: true
      }
    ]
}


if (!columnname.includes('producerId')){
wherequery = {
  [columnname[1]] : {
      [Op.like]: '%'+columnvalue[1] +'%'
    },
    [columnname[2]] : {
      [Op.like]: '%'+columnvalue[2] +'%'
    },
    [columnname[3]] : {
      [Op.like]: '%'+columnvalue[3] +'%'
    }
};
     includeRelation =    [{
        model: db.contracts,
        required: true
       
     }];
    }
    }

    if (req.body.tableName =='muncipalitys'){
        console.log("ProducerNamee::::" + req.body.columnname)
      // if (req.body.columnname == 'producer.name'){
      //   req.body.columnname = 'name';

      // }
      console.log("ProducerNamee:after if:::" + req.body.columnname);
      wherequery = {
        [columnname[1]] : {
          [Op.like]: '%'+columnvalue[1] +'%'
        },
        [columnname[2]] : {
          [Op.like]: '%'+columnvalue[2] +'%'
        },'$producer.name$':{
              [Op.like]: '%'+columnvalue[3]+'%'
            }
          }
         includeRelation =   [{
            model: db.producers,
            required: true,
            as: 'producer',
            all: true
          },
          {
            model: db.recyclers,
            required: true,
            as: 'recycler',
            all: true
          }
        ]
    }
console.log(includeRelation);


    let pageSize = 10;
  let page;
  if (req.query.page === undefined) {
    page = 0;
  } else {
    page = req.query.page * pageSize;
  }
db[req.body.tableName].findAndCountAll({
    offset: page,
    limit: pageSize,
    where: wherequery,
        include: includeRelation,
 
}).then(users => {
      
        if(users){
            console.log(users);
            res.status(200).json({status : true ,message: 'User Fetched ', users : users });
        }else{
            res.status(404).json({status : false ,message: 'No User'});
        }
    }).catch(err => console.log('err'));



}

0 个答案:

没有答案