【问题标题】:How to return AWS Lambda callback values with node js如何使用节点 js 返回 AWS Lambda 回调值
【发布时间】:2021-09-11 01:23:51
【问题描述】:

我对 AMS Lambda 有点陌生。

我编写了一个小(工作)函数来在 HarperDB 上执行一个简单的 sql 语句。 lambda 函数运行良好,日志显示来自 TableName

的结果行

问题是我如何从 SQL 语句的执行中获取或返回结果值。

以下是我的代码

exports.handler = (event, context, callback) => {
  
  //console.log("Event is ",event);
  //console.log("Context is ",context);
  //console.log("callback is",callback)
  
    var https = require('follow-redirects').https;
    var fs = require('fs');
    
    console.log(context); // for analysis purpose
    
    var options = {
      'method': 'POST',
      'hostname': 'MyInstance-MyAccount.harperdbcloud.com',
      'path': '/',
      'headers': {
        'Authorization': 'Basic MyAuthoriztionCode',
        'Content-Type': 'application/json'
      },
      'maxRedirects': 20
    };
    
    var req = https.request(options, function (res) {
      var chunks = [];
    
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });
    
      res.on("end", function (chunk) {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
      });
    
      res.on("error", function (error) {
        console.error(error);
      });
    });
    
    var postData = JSON.stringify({
      "operation": "sql",
      "sql": "SELECT * FROM MyInstanceName.TableName"
    });
    
    req.write(postData, callback);
    req.end();
    
    const response = {
        statusCode: 200,
        body: JSON.stringify('Select Completed!'),
    };
    
    return (response);  // I want to get the result of execution of my sql statement
    
    };

如何返回SQL语句的执行结果

【问题讨论】:

    标签: node.js database aws-lambda cloud


    【解决方案1】:

    在“结束”事件监听器中添加对回调的调用。

      res.on("end", function (chunk) {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
        callback(null, body.toString());
      });
    

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 2015-10-13
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      相关资源
      最近更新 更多