NodeJS中的AWS Lambda错误:“错误的参数”

时间:2019-11-25 05:13:50

标签: node.js amazon-web-services aws-lambda amazon-rds

场景

  • 我的React(Gatsby)应用程序从数据库请求信息以显示产品列表。
  • 该数据库是VPC中AWS RDS上的Postgres表。
  • 我的目标是让React应用程序触发AWS Lambda函数以从AWS RDS检索产品。

错误:

  • 在编写lambda函数时,我打算从表中请求所有产品。
  • 我收到的错误消息是TypeError: Wrong arguments

代码:

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"
  }
}

完整错误:

{
  "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)"
  ]
}

思路:我按照AWS指南上载了如何上传NodeJS部署程序包。本地测试时,connection.js似乎没有问题。

不确定如何调试它,因为即使“ AWS Lambda错误的参数”也不会产生相关结果。

1 个答案:

答案 0 :(得分:0)

我在这里看到的主要问题是您如何使用回叫。

回调函数接受两个参数error和value。

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

我认为您在这里做错的是,将函数作为引用而不是值传递给rds.getProducts()来获取值。