How to make multiple mysql queries in Node using promises

G'day all,

I am trying to convert some old PHP code to Node and part of the path is trying to find the best way to execute sql queries against my database (I am using SQL so that I can port an existing database).

They work for me, but ran into the "Pyramid of Doom" issue and subsequent scoping issues (ie return values ​​not having access to subsequent "then" s).

An example of such code I am here is: (dbPool.queryOPromise returns a query wrapped in a promise)

dbPool.queryOPromise(query)                                                                                     
.then(function(result){                                                                                         
    console.log(result);                                                                                          
    var query = {                                                                                                 
        sql:"INSERT INTO newusers (newuserid, ipaddress, email) VALUES (?,?,?)",                                    
        values: [newuserid, ipAddress, email]                                                                       
    };                                                                                                            
    dbPool.queryOPromise(query)                                                                                   
    .then(function(value){                                                                                        
        console.log(value);                                                                                         
        if(value.code==200) {                                                                                       
            res.status(200).json({code:200, status:"New User Created"});                                              
        } else {                                                                                                    
            res.status(400).json({code:value.code, status:"Error creating new user: ".value.status});
        }                                                                                                           
    })       
})

      

Does anyone have a look at the best way to attack this situation?

Thank!

+3


source to share


1 answer


It is supposed to < return the following promises instead of calling .then

on them

dbPool.queryOPromise(query)
.then(function(result) {
    console.log(result);
    var query = {
        sql: "INSERT INTO newusers (newuserid, ipaddress, email) VALUES (?,?,?)",
        values: [newuserid, ipAddress, email]
    };
    // RETURN the second promise, 
    return dbPool.queryOPromise(query);
})
.then(function(value) {
    console.log(value);
    if (value.code == 200) {
        res.status(200).json({code: 200, status: "New User Created"});
    } else {
        res.status(400).json({code: value.code, status: "Error creating new user: ".value.status });
    }
})
.catch(console.error); // and always catch the errors at the end. 

      



This is newbie mistake # 1 when using promises. Check out this excellently written article addressing issues like this

+5


source







All Articles