Before You start first go through this link for setting your Node.js API environment.
Here I will show you how to update one record using MySQL in node.js server. Follow this below procedure.
Write Update Query in Controller:
For updating one record in MySQL table write query in “employeecontroller.js” file in “controller” folder.
MySQL query for updating one record is :
update tbl_profile set name=‘Sitaram’,phone=’12345675443’,email=‘abc@gmail.com’ where id=1
Now use that query in the “employeecontroller.js” file in the “controller” folder.
Employeecontroller.js:
var db = require('../config/connection'); //reference of connection.js
var employees = {
Update: function (profile,id,callback) {
return db.query("update tbl_profile set name=?,phone=?,email=? where id=?",
[profile.name,profile.phone,profile.email,id], callback);
}
}
module.exports=employees;
Route Configuration for update employee records:
For configuring route for updating record in to employee profile table. Write route configuration code in “employeeroute.js” file in “route” folder.
Set PUT method to update employee profile details. Now use this following code in your “employeeroute.js” file in the “route” folder.
employeeroute.js:
var express = require('express');
var router = express.Router();
var employees = require('../controller/employeecontroller'); //Call your employee controller...
router.put('/update/:id', function (req, res, next) {
employees.Update(req.body,req.params.id,function (err, rows) {
if (err) {
res.send(err);
}
else {
res.send(rows);
}
})
})
module.exports = router;
OUTPUT:
Now open postman for checking that node.js application. Write your URL and set the method type is PUT, pass id through API URL and pass parameters through body in postman.
Click on the Send button then the output will be shown in the below window.
Check all the CRUD API following below link:
Here I will show you how to update one record using MySQL in node.js server. Follow this below procedure.
Write Update Query in Controller:
For updating one record in MySQL table write query in “employeecontroller.js” file in “controller” folder.
MySQL query for updating one record is :
update tbl_profile set name=‘Sitaram’,phone=’12345675443’,email=‘abc@gmail.com’ where id=1
Now use that query in the “employeecontroller.js” file in the “controller” folder.
Employeecontroller.js:
var db = require('../config/connection'); //reference of connection.js
var employees = {
Update: function (profile,id,callback) {
return db.query("update tbl_profile set name=?,phone=?,email=? where id=?",
[profile.name,profile.phone,profile.email,id], callback);
}
}
module.exports=employees;
Route Configuration for update employee records:
For configuring route for updating record in to employee profile table. Write route configuration code in “employeeroute.js” file in “route” folder.
Set PUT method to update employee profile details. Now use this following code in your “employeeroute.js” file in the “route” folder.
employeeroute.js:
var express = require('express');
var router = express.Router();
var employees = require('../controller/employeecontroller'); //Call your employee controller...
router.put('/update/:id', function (req, res, next) {
employees.Update(req.body,req.params.id,function (err, rows) {
if (err) {
res.send(err);
}
else {
res.send(rows);
}
})
})
module.exports = router;
OUTPUT:
Now open postman for checking that node.js application. Write your URL and set the method type is PUT, pass id through API URL and pass parameters through body in postman.
Click on the Send button then the output will be shown in the below window.
Check all the CRUD API following below link:
0 Comments