0

I am using the below code:

async function validateMOData(data, callback) {
    success('Validation started');
    let a = 0;
    let i = data.length;
    console.log('length of array', i);
    while (i--) {
        a = a + 1;
        console.log('Loop Count', a);
        console.log(data[i]);
        let result;
        try {
            result = await checkValidity(data[i]);
            if ((JSON.parse(result.body)).isValid === false) {
                createErrorLog(data[i].MemberId, data[i].MemberAgreementId, (JSON.parse(result.body)).responseMessage, (JSON.parse(result.body)).responseCode, 'x', data[i].CsvFileName)
                deleteFaultyData(data[i].Id);
                data.splice(i, 1);
            }
        } catch (err) {
            console.log(err);
        }
    }
    success('validation ended');
    callback(data);
}
function checkValidity(record) {
    return new Promise((resolve, reject) => {
        let validityCheckUrl = `${constants.USER_INTERFACE.SERVER_URL_PREFIX}validateMemberData/?memberId=${record.MemberId}&memberAgreementId=${record.MemberAgreementId}`;
        request({ url: validityCheckUrl, pool: { maxSockets: 10 } }, (error, response, body) => {
            if (error) {
                reject(error);
            } else {
                resolve(response, body);
            }
        });
    });

}

After a few iterations in the while loop, the checkValidity method starts to throw the below error :

{ Error: connect ECONNREFUSED IP:port
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: 'IP',
port: port }

This error does not come in the first request but after a few requests. Can anyone help? Am i missing anything?

NOte: This code run on AWS lambda and checkValidity connects to a Node.js server deployed on EC2

writeToBhuwan
  • 3,233
  • 11
  • 39
  • 67

1 Answers1

0

What i thinks is you did not placed a check to check that is the i is in negative or in simple words no break condition here inside try

try {
      ***********************
        result = await checkValidity(data[i]);
        if ((JSON.parse(result.body)).isValid === false) {
            createErrorLog(data[i].MemberId, data[i].MemberAgreementId, (JSON.parse(result.body)).responseMessage, (JSON.parse(result.body)).responseCode, 'x', data[i].CsvFileName)
            deleteFaultyData(data[i].Id);
            data.splice(i, 1);
        }
    } catch (err) {
        console.log(err);
    }

So the loop would trying to send the data inside the checkvalidity that is even not present so most probably it is sending error

mzparacha
  • 627
  • 6
  • 20