如何在 node.js lambda aws 中的 httpget request 中获取 mysql 数据


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;
};

我正在尝试从 http 请求中获取响应,其中包含结果[0].业务日期、结果[0].total_cash以及其他所有内容。但是我得到一个错误而不是输出。这是错误——

响应: { "错误类型": "引用错误", "错误消息": "未定义结果", "跟踪":[ "引用错误:未定义结果", " at Runtime.exports.handler (/var/task/index.js:45:27(", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25(" ] }

我认为这就是您要做的。如果我理解正确,您正在尝试查询 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;
};

注意:我还没有测试过这段代码,也不是一个完整的代码。

最新更新