[英]How to get mysql data in httpget request in node.js lambda aws
pool.getConnection(function(err, connection) {
var yesterday = new Date(Date.now() - 864e5);
var dd = yesterday.getDate();
var mm = yesterday.getMonth() + 1; //January is 0!
var yyyy = yesterday.getFullYear();
// Use the connection
connection.query("SELECT id, restaurant_id, revenue_id, revenue_name, total_cash,visa,mc,amex,
DATE_FORMAT(business_date, '%d-%M-%Y') as business_date, item_sales FROM `alfred-
prod`.report_day_sales where restaurant_id=19 and DATE_FORMAT(business_date, '%Y-%m-%d') =
DATE_FORMAT('2014-09-01', '%Y-%m-%d')", function (error, results, fields) {
console.log("SELECT id, restaurant_id, revenue_id, revenue_name, total_cash,visa,mc,amex,
DATE_FORMAT(business_date, '%d-%M-%Y') as business_date, item_sales FROM `alfred-
prod`.report_day_sales where restaurant_id=230 and DATE_FORMAT(business_date, '%Y-%m-%d') =
DATE_FORMAT('"+yyyy+"-"+mm+"-"+dd+"', '%Y-%m-%d')")})
});
const response = {
statusCode: 200,
body: JSON.stringify(
https.get('https://postman-echo.com/get?'+
'username ='+
'&password ='+
'&date=' + results[0].business_date +
'&cashSales='+ results[0].total_cash +
'&creditCardVisa='+ results[0].visa +
'&creditCardMaster='+ results[0].mc +
'&creditCardAmex=' + results[0].amex +
'&creditCardOthers=0',
res => {
//console.log(res.statusCode);
//console.log(res.headers);
let body = '';
res.on('data',data =>{
body += data;
})
res.on('end',()=>console.log(body));
})
),
};
return response;
};
I'm trying to get a response from the http request, with results[0].business date, results[0].total_cash and everything else.我正在尝试从 http 请求中获得响应,包括 results[0].business date、results[0].total_cash 和其他所有内容。 But I get an error instead of an output.
但是我收到错误而不是输出。 This is the error -
这是错误 -
Response: { "errorType": "ReferenceError", "errorMessage": "results is not defined", "trace": [ "ReferenceError: results is not defined", " at Runtime.exports.handler (/var/task/index.js:45:27)", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }响应:{ "errorType": "ReferenceError", "errorMessage": "results is not defined", "trace": [ "ReferenceError: results is not defined", " at Runtime.exports.handler (/var/task/index .js:45:27)", " 在 Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }
I think this is what you are trying to do.我认为这就是你想要做的。 If i am understanding you correctly, You are trying to query mysql to get the results and using that result to send an http request.
如果我正确理解您,您正在尝试查询 mysql 以获取结果并使用该结果发送 http 请求。
const https = require("https");
const getResults = () => {
const queryResponse = await new Promise((resolve, reject) => {
pool.getConnection(function (err, connection) {
var yesterday = new Date(Date.now() - 864e5);
var dd = yesterday.getDate();
var mm = yesterday.getMonth() + 1; //January is 0!
var yyyy = yesterday.getFullYear();
const query = "SELECT id, restaurant_id, revenue_id, revenue_name, total_cash,visa,mc,amex, DATE_FORMAT(business_date, '%d-%M-%Y') as business_date, item_sales FROM `alfred-\
prod`.report_day_sales where restaurant_id = 19 and DATE_FORMAT(business_date, '%Y-%m-%d') = DATE_FORMAT('2014-09-01', '%Y-%m-%d')"
// Use the connection
connection.query(query, (error, results, fields) => {
if (error) {
reject(error);
}
resolve(results)
})
});
});
return queryResponse;
}
exports.handler = async (event) => {
const httpResponse = await new Promise((resolve, reject) => {
const results = await getResults();
const { business_date, total_cash, visa, mc, amex } = results[0]
https.get(
`https://postman-echo.com/get?username=&password=&date=${business_date}&cashSales=${total_cash}&creditCardVisa=${visa}&creditCardMaster=${mc}&creditCardAmex=${amex}&creditCardOthers=0`,
resp => {
let body = '';
// A chunk of data has been recieved.
resp.on("data", chunk => {
body += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
resolve(body);
});
resp.on("error", error => {
reject(error);
});
}
);
});
const response = {
statusCode: 200,
body: JSON.stringify(httpResponse),
};
return response;
};
Note: I haven't tested this code, Also this is not a complete code.注意:我还没有测试过这段代码,这也不是一个完整的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.