提问者:小点点

在NodeJS Lambda函数中,如何从Axios.get这样的异步函数获得返回值?


我正在使用AWS Amplify在AWS中构建一些后端乐趣,我有一个Lambda函数,它是从对DynamoDB的更新中触发的。 它不需要返回任何东西。

我在CloudWatch中不断得到错误,说“syntaxError:await只在异步函数中有效”。 有没有其他方法在处理程序内运行这些异步函数?

exports.handler = async (event) => {
  console.log(JSON.stringify(event, null, 2));
  event.Records.forEach(record => {
    console.log(record.eventID);
    console.log(record.eventName);
    console.log('DynamoDB Record: %j', record.dynamodb);

    if (record.eventName == "INSERT") {
      try {
        res = await axios.get("https://google.com", {});
        console.log(res);
      }
      catch(e){
        console.log(e);
      }
    }
  });
};

共1个答案

匿名用户

您正在forEach循环内部执行等待,这是它自己的函数状态。 您可以尝试在if语句中使用内联异步等待函数

(async function process() {
    try {
        res = await axios.get("https://google.com", {});
        console.log(res);
    }
    catch(e){
        console.log(e);
    }
}())