I am new to node js. I am using express framework with sequelize orm to connect with my mysql database
Here's the one of the API Call I wrote
1 expressApp.post('/api/account_lister', (req, res) => {
2 var temp_obj = {}
3 var temp_list = []
4 WAAccount.findAll({where:{user_id:1}})
5 .then((DBData) => {
6 if ( DBData!== null ){
7 for (var i = 0; i < DBData.length; i++) {
8 let wa_account_name = DBData[i].dataValues.wa_account_name
9 let wa_id = DBData[i].dataValues.wa_id
10 WAProperties.findAll({where:{wa_id:wa_id}})
11 .then((DBData) => {
12 if ( DBData!== null){
13 for (var i = 0; i < DBData.length; i++) {
14 temp_list.push(DBData[i].dataValues.wa_property_name)
15 }
16 temp_obj[wa_account_name] = temp_list
17 temp_list = []
18 console.log(temp_obj)
19 }
20 })
21 }
22 }
23 })
24 console.log(temp_obj)
25 res.json({ success: true, account_property_list: temp_obj }); return;
26 })
In the Above Code, WAAccount and WAProperties are the two tables in MySQL.
In the above code, there is two then function. the function in the then is executed in async manner. So the variable temp_obj in line 24 returns empty. But in line 18, the temp_obj has values that were executed after line 24 due to it is inside in then function. I want the 24th line to be executed after the complete execution of then functions. how to make it. Help me with some solutions.
res.jsoninside.then()