Sunday, 9 April 2017

how to modify the value outside the then?

I use sequelize lib to retrieve data from MySQL and the function like:

let getVendorList = function(res,req){
    let tempList = [], vendor = [];
    Model.findAll({where....statement})
    .then((res)=>{
      for(let i = 0;i<res.length;i++){
            tempList.push(res[i].dataValues);
        }
     res.send(tempList)//here is correct but if I want to add somthing
     tempList.map((item)=>{
          sequelize.query('some query here',{
             type: sequelize.QueryTypes.SELECT})
             .spread(function(res){
                 item.a = res.a || 0;
                 item.b = res.b || 0;
                 vendor.push(item)//no matter I add return or not
             })   
        }).then((res)=>{
            console.log(vendor)//vendor is empty
        })
     })
})
//what I want is make consolt.log(vendor) is not empty here
}

Can anyone help for this ?many thanks



via Rick Z

No comments:

Post a Comment