2

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);
  })
}
kp99
  • 21
  • 2
  • Ultimately this error is because you're trying to connect to a PostgreSQL instance on the same machine that the code is running. Obviously on Lambda this won't be there. Where are you hosting your database? – stdunbar Apr 30 '20 at 22:18
  • @stdunbar, i'm hosting the db on RDS – kp99 May 01 '20 at 18:55
  • Where are you creating your connection to the database? You'll need to specify the hostname somewhere - it must default to localhost and not your RDS. – stdunbar May 01 '20 at 19:36
  • I create a new pg pool object and then import it at the top of the fille. I know that it's been hitting the RDS db because I have other endpoints that are working properly. I'll check to see if it's being overwritten anywhere though – kp99 May 01 '20 at 20:11
  • I stand corrected, my endpoints were working before, but now they all have this same error! – kp99 May 01 '20 at 20:34

0 Answers0