I've been stuck on this error for quite a long time, and am reaching out to see if anyone knows what to do!
I'm making an endpoint using Lambda that I want to, when hit, first insert a contract into a postgres db running on RDS and then insert the contents of a message into the same database (and send an email using SES but I figured that part out already). What I want to do, is pass a new postgres pool into the send message function down below, but every time that I do, and try to reach the database I get a Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
. This is when I run serverless offline. The initial SQL query runs but the second one doesn't.
I would really appreciate any guidance, I've pasted the relevant code snippets below. This is my first time doing anything with JavaScript, so this might be a very dumb question
contracts.js
const db = require('../db_connect');
const messages = require('./messages');
//POST endpoint to create a generic contract, it's worth keeping in mind that whenever we create a contract, it will always default to false for field isFinished
module.exports.createContract = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const reqBody = JSON.parse(event.body);
const influencerID = reqBody.influencerID;
const buyerID = reqBody.buyerID;
const monetaryValue = reqBody.monetaryValue;
const charityID = reqBody.charityID;
db.query('INSERT INTO contracts(influencer_id, buyer_id, monetary_value, charity_id, is_finished) VALUES($1, $2, $3, $4, false)', [influencerID, buyerID, monetaryValue, charityID])
.then(res => {
callback(null, {
statusCode: 200,
body: 'successfully inserted contract'
})
messages.sendMessage(db, reqBody, buyerID, influencerID)
})
.catch(e => {
console.log(e);
callback(null, {
statusCode: e.statusCode || 500,
body: 'Error:' + e
});
});
};
messages.js
var dotenv = require('dotenv').config();
var aws = require('aws-sdk')
const db = require('../db_connect');
function sendMessage(reqBody, senderID, receiverID) {
const messageBody = reqBody.messageBody;
const timestamp = Date.now();
var receiverEmail;
db.query("SELECT e_mail FROM users WHERE user_id = $1", [receiverID])
.then(res => {
console.log(res)
receiverEmail = res.rows[0]
console.log("this is the new email --> ", receiverEmail)
})
.catch(e =>{
throw console.error(e);
})
}