【问题标题】:Nodejs lambda function api call returns falseNodejs lambda函数api调用返回false
【发布时间】:2019-11-01 01:23:07
【问题描述】:

我在我的 AWS lamda 控制台上运行了以下代码。代码完成得很好,但没有进行 api 调用。你知道这段代码有什么问题或者如何跟踪日志吗?

   var http = require('https');

   exports.handler = function (event, context) {

   const _requestData = JSON.stringify('{"Name":"AWS Lambda"}');

   console.log(event +' ____event ' +_requestData + ' ______requestData');

   const _options = {
             host: "login.salesforce.com",
             path: "/services/data/v20.0/sobjects/Case/",
              method: 'POST',
              port: 80,
              headers: {
                'grant_type' : 'authorization_code', 
                'client_id':'xxxxxx',
                'client_secret':'xxxxxxx',
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(JSON.stringify(_requestData))
              }

  };

  var post_req = http.request(_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response Chunk: ' + chunk);
      });
   });
   console.log('post_req' + JSON.stringify(post_req));
   var post_response = post_req.write(_requestData);
   console.log('post_response '+post_response);
   post_req.end();
   context.succeed("done");

}

【问题讨论】:

    标签: node.js api authentication aws-lambda salesforce


    【解决方案1】:

    看来你是 Node 的初学者,没关系,你犯的错误很少。 这行var post_response = post_req.write(_requestData) 不会给你最终结果,而是返回true 如果写入成功。你可以像这样改变函数:

    var http = require('https');
    exports.handler = function(event, context) {
    
        const _requestData = JSON.stringify('{"Name":"AWS Lambda"}');
    
        console.log(event + ' ____event ' + _requestData + ' ______requestData');
    
        const _options = {
            host: "login.salesforce.com",
            path: "/services/data/v20.0/sobjects/Case/",
            method: 'POST',
            port: 80,
            headers: {
                'grant_type': 'authorization_code',
                'client_id': 'xxxxxx',
                'client_secret': 'xxxxxxx',
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(JSON.stringify(_requestData))
            }
    
        };
    
        var post_req = http.request(_options, function(res) {
            let post_response;
            let chunks = []
            res.setEncoding('utf8');
            res.on('data', function(chunk) {
                console.log('Response Chunk: ' + chunk);
                chunks.push(chunk)
            });
            res.on('end', function() {
                post_response = JSON.parse(Buffer.concate(chunks).toString())
                // once request done send lambda response
                context.succeed("done");
            })
        });
        post_req.end();
        pos_req.on('error', function(err) {
            context.succeed("failed");
        })
    
    }
    

    如果您是 Node 初学者,我建议您学习异步编程、流、回调、promise,当然还有 async-await。

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多