简体   繁体   English

Lambda function 未插入 Postgres RDS

[英]Lambda function not inserting into Postgres RDS

I am trying to insert a record into RDS Postgres using node.js lambda function.我正在尝试使用 node.js lambda function 将记录插入 RDS Postgres。 I can't tell if it is connecting to the database or not.我不知道它是否连接到数据库。 It did insert 3 records once and then nothing.它确实插入了 3 条记录,然后什么也没有。 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:日志 output 是:

INFO test id:820982911946154500信息测试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) INFO 错误:客户端已关闭且无法在 /opt/nodejs/node_modules/pg/lib/client.js:570:27 在 processTicksAndRejections (internal/process/task_queues.js:79:11) 处查询

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

Resolved this after changing to pg.Pool() to manage the connections based on this post - Node.js - PostgreSQL (pg): Client has already been connected.在更改为 pg.Pool() 以管理基于此帖子的连接后解决此问题 - Node.js - PostgreSQL (pg):客户端已连接。 You cannot reuse a client 您不能重用客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM