【问题标题】:AWS Lambda nodejs mysql query inside loop is not working循环内的AWS Lambda nodejs mysql查询不起作用
【发布时间】:2021-03-19 14:31:08
【问题描述】:
module.exports.handler = async (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  let index = 0;
    var client = mysql.createConnection({
      host: process.env.rds_host,
      user: process.env.rds_username,
      password: process.env.rds_password,
      port: process.env.rds_port
    });
    client.connect((err) => {
      if (err) throw err;
      console.log("Connected!");
    });
  let array = [];
  let queries = ["query1", "query2", "query3", "query4"];
  queries.map(q => {
    array.push(getQueryResult(client, q));
  });
  Promise.all(array).then(result => {
    console.log(result);
  });
  callback(null, {});
};

const getQueryResult = async (client, query) => {
  return new Promise((resolve, reject) => {
    client.query(query, function (err, result) {
      if (err) {
        reject(err);
      }
      resolve(result);
    });
  });
};

以上是我的 lambda 脚本,用于从 mysql 执行多个查询。问题是我没有从上面的脚本中得到任何结果和错误消息。请帮助我,我的脚本中缺少什么?

【问题讨论】:

  • 等等 client.connect 你没有得到日志“连接!?
  • @rags2riches 是的,我还没有看到“已连接”。
  • 这里有两个可能的问题:1) if 语句未评估,因为您使用的是“未定义”,而这意味着 undefined 作为原始类型 2) 您不是 await与数据库的连接
  • 评估typeof客户端的目的是什么?客户端未在 if 之前声明或分配,因此您希望它未定义...为什么如果需要?
  • @rags2riches undefined 表示我不想像使用现有连接一样再次打开连接。在 promises.all 中,我们不需要将await 放在那里。

标签: mysql node.js aws-lambda


【解决方案1】:

问题是 >> 代码没有等待完成 Promise

您可以通过以下方式解决:

在 then 中添加回调

Promise.all(array).then(result => {
    console.log(result);
    callback(null, {});
  });

使用等待

let result = await Promise.all(promiseArray);
console.log(result)
callback(null, {});

注意:使用 try-catch 处理 await

中的错误

另外,不要使用map 来循环数组,而是使用For loop

【讨论】:

    【解决方案2】:

    上面的代码存在两个或三个(潜在)问题:

    1. 由于typeof 客户端谓词未返回 true,因此您的 if 语句未得到评估。

    2. 您的mysql 端口与您的localhost 端口冲突(假设)

    这样改变你的 if 块:

     // check if `dotenv` has been called if you use it
     require('dotenv').config();
     
     // access the state property on mysql object
     // if you have closed your connection previously
     // this will get evaluated by the parser
     if (mysql.state === "disconnected") {  
     
          var client = mysql.createConnection({
              host: process.env.rds_host,
              user: process.env.rds_username,
              password: process.env.rds_password,
              port: process.env.rds_port        // is your port the same as your localhost?
              // include your database name here
            });
         
          // I suggest you to await your client connection
          // since mysql executes sequentially, not asynchronously
          client.connect(function(err) => {
              if (err) {
                console.error('error connecting: ' + err.stack);
                return;
              }
              console.log("Connected!");
            });
     }
    

    如果错误仍然存​​在,则表示您的环境变量设置不正确,因此应检查您的数据库配置(请参阅内联 cmets)。

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2021-09-13
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多