0

Can anyone help me out why my function returning undefined? Here is my function

async function getPlaceIDByPincode(city, state, pincode)
{
    let sql = "select * from place_master where pincode='"+pincode+"'";
        await db.query(sql,(err,data)=>{
            
            if(data.length)
            {
                return data[0].id;
            }
            else
            {
                
                let query = `INSERT INTO place_master 
                    (city,state, pincode) VALUES (?,?,?);`;
                    let values = [
                           city.toUpperCase(), state.toUpperCase(),pincode 
                    ];
                    
                    db.query(query,values, (err,rows)=>{
                        if(err)
                        {
                            console.log("Hii");
                            if(err.code=="ER_DUP_ENTRY")
                            {
                                return false; 
                            }
                
                        }
            
                        return rows.insertId;
                    });
            }
        });
    }

Here is function calling

getPlaceIDByPincode(city,state,pincode).then(function(result){
          console.log(result); // returning undefined
        });
Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
  • Does this answer your question? [Use promise to process MySQL return value in node.js](https://stackoverflow.com/questions/36547292/use-promise-to-process-mysql-return-value-in-node-js) – Ivar Feb 06 '22 at 18:40
  • Also related: [How do I convert an existing callback API to promises?](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – Ivar Feb 06 '22 at 18:41
  • As a side note: You are using a parameterized query for your insert. You should do the same for your select query. Otherwise you could be vulnerable to SQL Injection attacks. – Ivar Feb 06 '22 at 18:42

0 Answers0