Advertisement
kratis

node

Nov 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. var mysql = require('mysql');
  2. var AWS = require('aws-sdk');
  3. //var promise = require('promise');
  4. var S3= AWS.S3;
  5. var connection = mysql.createConnection({
  6. host: "",
  7. user: "",
  8. password: "",
  9. database: "",
  10. port : ""
  11. });
  12.  
  13.  
  14. exports.handler = (event, context,callback) => {
  15. console.log(event);
  16.  
  17.  
  18. var jsondata = event.state.reported
  19. var values = [];
  20. console.log("length"+Object.keys(jsondata).length);
  21.  
  22. function pushvalue(err,result){
  23. if(err){
  24. console.log(err);
  25. }
  26. else{
  27. for(var j=1; j<=Object.keys(jsondata).length; j++){
  28. var data= jsondata[j];
  29. console.log(data);
  30. values.push([data.Day, data.Meter, data.KWH, data.KW, data.Ampere, data.PF,data.VLL]);
  31. }
  32. console.log("here");
  33. return(values);
  34. }
  35. }
  36.  
  37. //Bulk insert using nested array [ [a,b],[c,d] ] will be flattened to (a,b),(c,d)
  38. //the problem lies here and the structure of this function, i want to call the pushvalue function and when the result (values) is //arrived, only then the next connection.query should be operable
  39.  
  40. pushvalue().then(result =>{
  41. connection.query('INSERT INTO Energydata (Day, Meter, KWH, KW, Ampere, PF, VLL) VALUES ?', [result], function(error,results) {
  42. if (error) {
  43. console.log(error);
  44.  
  45. } else {
  46. console.log(results+"here");
  47. callback(error, results);
  48. connection.end(function (err) { callback(err, results);});
  49. }
  50. });
  51. });
  52. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement