0

I am trying to develop an AWS lambda function with Node.JS. I am actually building a REST API with AWS Lambda, API Gateway and AWS RDS(MySQL). Please check the below code

var response;

exports.lambdaHandler = async (event, context) => {

    var mysql = require('mysql');
    var con = mysql.createConnection({
        host: "***",
        user: "***",
        password: "***",
        database: "aaaa",
        port: 3306
    });

    con.connect();

    try {
        conn.query('SELECT * from accounting_type', function (err, data) {
            if (err) console.log(err);

            console.log(data);

            conn.close(function () {
                console.log('done');
            });

            response = {
                'statusCode': 200,
                'body': JSON.stringify({
                    data,
                })
            }

        });
    } catch (err) {
        console.log(err);
        return err;
    }

    return response;

};

In this code, the response I get is just 1.

However, the output I need is this.

[
    {
        "idaccountingType": 1,
        "type": "credit"
    },
    {
        "idaccountingType": 2,
        "type": "debit"
    }
]

This is the first time I am using Node.JS, What am I doing wrong?

Barmar
  • 741,623
  • 53
  • 500
  • 612
PeakGen
  • 21,894
  • 86
  • 261
  • 463

0 Answers0