1
votes

In this day I have a problem with aws lambda. I need to search in a RDS postgres db by passing to lambda a username, but during every test event it return:

"{
  "errorMessage": "RequestId: 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 Process exited before completing request"
}"

In particular this is the stack returnes:

START RequestId: 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 Version: $LATEST 2019-03-31T12:32:19.572Z 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 TypeError: Path must be a string. Received undefined at assertPath (path.js:28:11) at Object.join (path.js:1236:7) at Object.module.exports.getFileName (/var/task/node_modules/pgpass/lib/helper.js:61:16) at module.exports (/var/task/node_modules/pgpass/lib/index.js:10:23) at Connection. (/var/task/node_modules/pg/lib/client.js:110:9) at emitOne (events.js:116:13) at Connection.emit (events.js:211:7) at Socket. (/var/task/node_modules/pg/lib/connection.js:125:12) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) END RequestId: 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 REPORT RequestId: 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 Duration: 583.72 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 35 MB
RequestId: 070f76a4-41c6-40d1-8ef2-0a84d9c04af0 Process exited before completing request

const AWS = require('aws-sdk');

global.fetch = require('node-fetch');

var Pool = require('pg').Pool;

const psql = new Pool ({

host: '',
user: '',
password: '',
database: 'metadata',
port: 5432
})

function searchUser(username){

return new Promise((resolve, reject) => {

    psql.query("SELECT username FROM utilitator where username LIKE '%$1%'", [username], {

        onSucces: function(res){
            resolve(res.rows);
        },
        onFailure: function(err){
            resolve("err");
        },
    });
});
}

exports.handler = async (event, context, callback) => {
//var body = JSON.parse(event.body);

var username = event.username;

var response = {
    "statusCode": 200,
    "isBase64Encoded": false,
    "body": {},
}

try{
    var result = await searchUser(username).then((result) => {
        return result;
    });

    var statusCode = 200;
    var body = result;
    if(result == "err"){
        statusCode = 400;
        body = "user not found";
    }

    response.statusCode = statusCode;
    response.body = body;

    callback(null,response);
}
catch(e){
    callback(e,{
        "isBase64Encoded": false,
        "headers": {},
        "body": "err",
        "statusCode": 501
    });
}
};

I expect that Lambda return all user in db that have a similar username that I have passed.

1

1 Answers

0
votes

I was browsing through the node-postgres docs and I did not find any reference to onSuccess and onFailure events, but only err and res objects in the callback (but I may have overlooked it):

client.query(text, values, (err, res) => {
  if (err) {
    console.log(err.stack)
  } else {
    console.log(res.rows[0])
    // { name: 'brianc', email: '[email protected]' }
  }
})

However, it also supports a promise version, meaning you don't have to promifisy it yourself:

try {
  const res = await pool.query(text, values)
  console.log(res.rows[0])
  // { name: 'brianc', email: '[email protected]' }
} catch(err) {
  console.log(err.stack)
}

This means your searchUser function should look like this:

const searchUser = username => {
  return await psql.query('SELECT username FROM utilitator where username LIKE $1', [`%${username}%`])
};

Finally, on your handler, since you are already using Node 8, you should not mix promises and callbacks together. Simply return the body in a stringified JSON inside your response object, like this (error handling omitted for the sake of simplicity):

exports.handler = async (event) => {
  //const body = JSON.parse(event.body);

  const username = event.username;

  const result = await searchUser(username)

  if (result == "err") {
    return {
      "statusCode": 404,
      "isBase64Encoded": false,
      "body": JSON.stringify({message: 'User not found'}),
    }
  }
  return {
    "statusCode": 200,
    "isBase64Encoded": false,
    "body": JSON.stringify(result),
  }

};