I am trying to insert a record into RDS Postgres using node.js lambda function. I can't tell if it is connecting to the database or not. It did insert 3 records once and then nothing. Nothing is being written to the log. Below is my code:
'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 ($1, $2, $3, $4)",
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;
};
The log output is:
INFO test id:820982911946154500
INFO Error: Client was closed and is not queryable at /opt/nodejs/node_modules/pg/lib/client.js:570:27 at processTicksAndRejections (internal/process/task_queues.js:79:11)
Any help is appreciated. Thanks.