简体   繁体   中英

How to get column name of data from RDS database as response in JSON in AWS Lambda Function using Nodejs

 "records": [
      [
        {
          "stringValue": "Smile Foundation2"
        },
        {
          "stringValue": "Mumbai"
        },
        {
          "stringValue": "+918600250073"
        },
        {
          "stringValue": "Children"
        },
        {
          "stringValue": "Alandi Pune ,411021"
        },

So I get this response but the key name is the same for all I want it as different.

You can use a simple select query to fetch all the required field names and their values

I am adding a Node.js code below for reference that fetches data as JSON in response from database using AWS Lambda Function .

var mysql = require ('mysql');

var pool  = mysql.createPool
({ 
    host     : 'your RDS endpoint',
    database : 'your database name',
    user     : 'your database username',
    password : 'your database password',
});

exports.handler = (event, context, callback) =>

{
    context.callbackWaitsForEmptyEventLoop = false;
    
    pool.getConnection(function(error, connection) 
    {
    
        connection.query ("SELECT column_name FROM table_name", function(error, results, fields) 
        {
            connection.release();
            if (error) callback(error);
            else callback(null, JSON.stringify(results));
        });
  
  });
  
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM