0

I send two query sequentially Query the data from A tables, and then accoring to the result, query the data from B table.

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. But in lambda, put the function into exports.handler, it response null. If I change the 'return result' to callback(result), it occurs error. I think it maybe too simple to solve this problem If you know about that, please help me

1 Answer 1

1

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. 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". 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). So basically on your line before console.log('done'); use callback(null, result) and it will work for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.