0

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.

1
  • Move res.json inside .then() Commented Mar 26, 2020 at 7:38

1 Answer 1

1

You could do:

expressApp.post('/api/account_lister', async (req, res) => {
    var temp_obj = {}
    var temp_list = []
    const DBData = await WAAccount.findAll({where:{user_id:1}})   
    if ( DBData!== null ){
        for (var i = 0; i < DBData.length; i++) {
            let wa_account_name = DBData[i].dataValues.wa_account_name
            let wa_id = DBData[i].dataValues.wa_id
            const DBData2 = await WAProperties.findAll({where:{wa_id:wa_id}})
            if ( DBData2!== null){
                for (var i = 0; i < DBData2.length; i++) {
                    temp_list.push(DBData2[i].dataValues.wa_property_name)
                }
                temp_obj[wa_account_name] = temp_list
                temp_list = []
                console.log(temp_obj)          
            }
        }
    }
    console.log(temp_obj)   
    res.json({ success: true, account_property_list: temp_obj }); return;
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.