简体   繁体   English

Node.js 8.10中具有异步瀑布的AWS Lambda查询

[英]AWS Lambda query with async waterfall in node.js 8.10

I send two query sequentially Query the data from A tables, and then accoring to the result, query the data from B table. 我依次发送两个查询,从A表中查询数据,然后根据结果,从B表中查询数据。

So, I query the data like that, 所以,我这样查询数据

var async = require('async');
var mysql = require('mysql');
var config = require('./config.json');
var connection = mysql.createConnection({
  host     : config.dbhost,
  user     : config.dbuser,
  password : config.dbpassword,
  database : config.dbname
});

exports.handler = (event, context, callback) => {
    // TODO implement
    var tasks = [
        function (callback) {
            connection.query("SELECT email FROM Visitor WHERE id =?;", [1], function (err, row) {
                if (err) return callback(err);
                if (row.length == 0) return callback('No Result Error');
                callback(null, row[0]);
            })
        },
        function (data, callback) {
            connection.query("SELECT id,signtime FROM Board WHERE email =?;", data.email, function (err, row) {
                if (err) return callback(err);
                if (row.length == 0) {
                  return callback('No Result Error');
                }else {
                  callback(null, row[0])
                }
            })
        }
    ];

    async.waterfall(tasks, function (err, result) {
        if (err)
          console.log('err');
        else
          ***return result;***
          console.log('done');
        connection.end();
    });
};

I log the data with console.log(), it take the data in command line. 我使用console.log()记录数据,它在命令行中获取数据。 But in lambda, put the function into exports.handler, it response null. 但是在lambda中,将函数放入exports.handler中,它的响应为空。 If I change the 'return result' to callback(result), it occurs error. 如果将“返回结果”更改为callback(result),则会发生错误。 I think it maybe too simple to solve this problem If you know about that, please help me 我认为解决这个问题可能太简单了。如果您知道这一点,请帮助我

In the first case, response is null because you didn't use neither Promise, nor callback to let the Lambda sandbox know that the job is done. 在第一种情况下,response为空,因为您既没有使用Promise,也没有使用回调来让Lambda沙盒知道该工作已完成。 In the second case, you used the callback, but you passed the result as the first argument to it. 在第二种情况下,您使用了回调,但是将结果作为第一个参数传递给了它。 Lambda programming model for Node.js follows a principle called "error first callback". Node.js的Lambda编程模型遵循称为“错误优先回调”的原则。 Long story short, if any error occurred during execution, you should go with callback(error) , and if everything is ok and you need to return some result from lambda, you should go with callback(null, result) . 长话短说,如果执行期间发生任何错误,则应使用callback(error) ,如果一切正常,并且需要从lambda返回一些结果,则应使用callback(null, result) So basically on your line before console.log('done'); 所以基本上就在console.log('done'); use callback(null, result) and it will work for you. 使用callback(null, result) ,它将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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