Created
August 5, 2018 15:24
-
-
Save tankala/650bed87c9fd65e9792ec9857127fe47 to your computer and use it in GitHub Desktop.
Updating user address with the passing a variable to callback function concept using bind()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require('mysql'); | |
var con = mysql.createConnection({ | |
host: "localhost", | |
user: "root", | |
password: "root", | |
database: "Employee" | |
}); | |
con.connect(function (err) { | |
if (err) throw err; | |
console.log('Connected'); | |
}); | |
var getEmpObj = function (empName, callback) { | |
let seletQuery = "SELECT * FROM Employee where employeeName = '" + empName + "'"; | |
con.query(seletQuery, function (err, result, fields) { | |
if (err) throw err; | |
callback(result); | |
}); | |
} | |
var updateEmpAddress = function (empName, empNewAddress, callback) { | |
getEmpObj(empName, function(result) { | |
let empId = result[0].employeeId; | |
let updateAddressQuery = "UPDATE Employee SET employeeAddress = '" + empNewAddress + "' WHERE employeeId = " + empId; | |
con.query(updateAddressQuery, function (err, result) { | |
if (err) throw err; | |
console.log(result.affectedRows + " record(s) updated"); | |
callback(); | |
}); | |
}); | |
} | |
var printEmployeeDetails = function() { | |
getEmpObj(this.empName, function(result) { | |
console.log(JSON.stringify(result[0])); | |
con.end(); | |
}); | |
} | |
updateEmpAddress("Aditya", "Delhi", printEmployeeDetails.bind({"empName": "Aditya"})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment