Lambda 函数未插入到 Postgres RDS 中
Lambda function not inserting into Postgres RDS
我正在尝试使用 node.js lambda 函数将记录插入 RDS Postgres。我不知道它是否连接到数据库。它确实插入了 3 条记录,然后什么也没有。没有任何内容被写入日志。下面是我的代码:
'use strict';
const { Client } = require('pg') ;
const client = new Client();
client.connect();
exports.handler = async function(event, context) {
var response;
if (event.Records) {
for (let record of event.Records) {
var rec = JSON.parse(JSON.stringify(record.body));
var test = JSON.parse(rec);
console.log("test id:" + test.id);
const queryString = {
text: "INSERT INTO public.order VALUES (, , , )",
values: [test.id, test.email, test.total_price, test.token],
};
try{
const result = await client.query(queryString);
console.log(result.rowCount);
response = result.rowCount;
}
catch (err) {
console.log(err.stack);
}
}
client.end();
}
return response;
};
日志输出为:
信息测试id:820982911946154500
INFO 错误:客户端已关闭,无法在 /opt/nodejs/node_modules/pg/lib/client.js:570:27 at processTicksAndRejections (internal/process/task_queues.js:79:11)
查询
感谢任何帮助。谢谢
更改为 pg.Pool() 以管理基于此 post 的连接后解决了此问题 -
我正在尝试使用 node.js lambda 函数将记录插入 RDS Postgres。我不知道它是否连接到数据库。它确实插入了 3 条记录,然后什么也没有。没有任何内容被写入日志。下面是我的代码:
'use strict';
const { Client } = require('pg') ;
const client = new Client();
client.connect();
exports.handler = async function(event, context) {
var response;
if (event.Records) {
for (let record of event.Records) {
var rec = JSON.parse(JSON.stringify(record.body));
var test = JSON.parse(rec);
console.log("test id:" + test.id);
const queryString = {
text: "INSERT INTO public.order VALUES (, , , )",
values: [test.id, test.email, test.total_price, test.token],
};
try{
const result = await client.query(queryString);
console.log(result.rowCount);
response = result.rowCount;
}
catch (err) {
console.log(err.stack);
}
}
client.end();
}
return response;
};
日志输出为:
信息测试id:820982911946154500
INFO 错误:客户端已关闭,无法在 /opt/nodejs/node_modules/pg/lib/client.js:570:27 at processTicksAndRejections (internal/process/task_queues.js:79:11)
查询感谢任何帮助。谢谢
更改为 pg.Pool() 以管理基于此 post 的连接后解决了此问题 -