Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var AWS = require('aws-sdk');
- //var promise = require('promise');
- var S3= AWS.S3;
- var connection = mysql.createConnection({
- host: "",
- user: "",
- password: "",
- database: "",
- port : ""
- });
- exports.handler = (event, context,callback) => {
- console.log(event);
- var jsondata = event.state.reported
- var values = [];
- console.log("length"+Object.keys(jsondata).length);
- function pushvalue(err,result){
- if(err){
- console.log(err);
- }
- else{
- for(var j=1; j<=Object.keys(jsondata).length; j++){
- var data= jsondata[j];
- console.log(data);
- values.push([data.Day, data.Meter, data.KWH, data.KW, data.Ampere, data.PF,data.VLL]);
- }
- console.log("here");
- return(values);
- }
- }
- //Bulk insert using nested array [ [a,b],[c,d] ] will be flattened to (a,b),(c,d)
- //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
- pushvalue().then(result =>{
- connection.query('INSERT INTO Energydata (Day, Meter, KWH, KW, Ampere, PF, VLL) VALUES ?', [result], function(error,results) {
- if (error) {
- console.log(error);
- } else {
- console.log(results+"here");
- callback(error, results);
- connection.end(function (err) { callback(err, results);});
- }
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement