Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda in NodeJS error: "Wrong arguments"

Scenario

  • My React (Gatsby) application requests information from a database to display a list of products.
  • The database is a Postgres table on AWS RDS, in a VPC.
  • My aim is for the React application to trigger an AWS Lambda function to retrieve products from AWS RDS.

Error:

  • In writing my lambda function, I intend to request all products from my table.
  • The error message I get is TypeError: Wrong arguments

Code:

index.js

const rds = require('./connection.js');

exports.handler = async ( event, context, callback ) => {
    await callback(null, rds.getProducts);
};

connection.js

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

const pool = new Pool({
    user: process.env.user,
    host: process.env.host,
    database: process.env.database,
    password: process.env.password,
    port: process.env.port,
});

const getProducts = ( request, response ) => {
    pool.query(`SELECT * FROM product_list ORDER by id ASC`, ( error, result ) => {
        if( error ) throw new Error(error);
        response.status(200).json(result.rows);
    })
};

module.exports = {
    getProducts,
};

package.json

{
  "name": "lambda3",
  "version": "1.0.0",
  "description": "lambda function access rds",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "scott",
  "license": "ISC",
  "dependencies": {
    "pg": "^7.14.0"
  }
}

Full error:

{
  "errorType": "TypeError",
  "errorMessage": "Wrong arguments",
  "trace": [
    "TypeError: Wrong arguments",
    "    at RAPIDClient.postInvocationResponse (/var/runtime/RAPIDClient.js:41:18)",
    "    at complete (/var/runtime/CallbackContext.js:34:12)",
    "    at callback (/var/runtime/CallbackContext.js:44:7)",
    "    at /var/runtime/CallbackContext.js:105:16",
    "    at Runtime.exports.handler (/var/task/index.js:9:11)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Thoughts: I followed the AWS guide on how to upload a NodeJS deployment package. There didn't seem to be an issue with connection.js when testing locally.

Unsure how to debug this as even "AWS Lambda wrong arguments" yields few relevant results.

like image 596
Scott Avatar asked Mar 01 '26 03:03

Scott


1 Answers

What I see as the main issue here is how you use the call back.

The callback function accepts two parameters error and the value.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

What I think that you are doing wrong here is, passing the function as reference instead of value for e.g rds.getProducts() to get the value.

like image 142
Arun Kamalanathan Avatar answered Mar 03 '26 17:03

Arun Kamalanathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!