Advertisement
Virajsinh

Node.js Village Pin Code Update

Nov 6th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | Source Code | 0 0
  1. Open Command Line.
  2.  
  3. # Initialize npm
  4. 1. npm init -y
  5. 2. create run.js file and copy belove code
  6. 3. node run.js
  7. --------------------------------------------------------------
  8. const mysql = require('mysql2');
  9. const axios = require('axios');
  10.  
  11. // Create a connection to the MySQL database
  12. const connection = mysql.createConnection({
  13. host: 'localhost',
  14. user: 'root',
  15. password: '',
  16. database: 'practice_db'
  17. });
  18.  
  19. // Function to get the pin code from the Postal API
  20. async function getPinCodeFromAPI(villageName) {
  21. try {
  22. const response = await axios.get(`https://api.postalpincode.in/postoffice/${encodeURIComponent(villageName)}`);
  23. if (response.data && response.data[0].Status === 'Success') {
  24. const pinCode = response.data[0].PostOffice[0].Pincode;
  25. return pinCode; // Return the found pin code
  26. } else {
  27. console.log(`No pin code found for village: ${villageName}`);
  28. return null; // Return null if no pin code found
  29. }
  30. } catch (error) {
  31. console.error('Error fetching pin code from API:', error.message);
  32. return null; // Return null in case of an error
  33. }
  34. }
  35.  
  36. // Function to fetch data from the database and update pin codes
  37. async function updatePinCodes() {
  38. try {
  39. // Fetch data from the database
  40. const [rows] = await connection.promise().query(`
  41. SELECT s.state_name, d.dist_name, sd.sub_dist_name, v.village_name, v.village_id
  42. FROM tbl_state AS s
  43. LEFT JOIN tbl_dist AS d ON s.state_id = d.state_id
  44. LEFT JOIN tbl_sub_dist AS sd ON sd.dist_id = d.dist_id
  45. LEFT JOIN tbl_village AS v ON v.sub_dist_id = sd.sub_dist_id
  46. WHERE s.status = 'active'
  47. `);
  48.  
  49. // Iterate over each village and get the pin code
  50. for (const row of rows) {
  51. const villageName = row.village_name;
  52. console.log(`Checking pin code for village: ${villageName}`);
  53.  
  54. // Fetch the pin code from the API
  55. const pinCode = await getPinCodeFromAPI(villageName);
  56.  
  57. if (pinCode)
  58. {
  59. // If a pin code is found, update the database
  60. console.log(`Updating pin code for ${villageName} to ${pinCode}`);
  61.  
  62. // Update the pin code in the tbl_village table
  63. await connection.promise().query(
  64. 'UPDATE tbl_village SET pin_code = ? WHERE village_id = ?',
  65. [pinCode, row.village_id]
  66. );
  67. }
  68. }
  69.  
  70. console.log('Pin code update process completed!');
  71. } catch (err) {
  72. console.error('Error:', err.message);
  73. } finally {
  74. // Close the connection
  75. connection.end();
  76. }
  77. }
  78.  
  79. // Run the update process
  80. updatePinCodes();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement