【问题标题】:Internal server error aws lambda function nodejs内部服务器错误 aws lambda 函数 nodejs
【发布时间】:2020-07-07 16:34:48
【问题描述】:

我正在尝试使用 Axios 和 Cheerio 使用 AWS lambda 函数进行演示,在将端点调用为 {message: Internal Server Error} 后我得到了响应

exports.lambdaHandler = async (event, context) => {
    try {

       const axios = require('axios');
       const cheerio = require('cheerio');
         axios.get('https://www.kitco.com').then((response) => {
            const html = response.data;
            const $ = cheerio.load(html);
            const ask = $('#AU-ask').text();
            const bid = $('#AU-bid').text();
            const resbid = bid.slice(0,7);
            const resask = ask.slice(0,7);
            const result = {
                "ask": resask,
                "bid": resbid
            }
            return result;

        }); 
        response = {
            'statusCode': 200,
            'body': result
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    return response 

};

【问题讨论】:

  • 处理程序被调用了吗?致电您的处理程序handler docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html。您能否提供有关错误的更多描述?
  • 是的,内部服务器的错误是在浏览器中单击 API Gateway 端点链接之后,该链接又调用处理程序
  • 打电话给你处理程序handler遵守API很重要。让我知道它是否有效。
  • 刚试过,同样的问题,响应是{message:Internal Server Error}

标签: javascript node.js express aws-lambda aws-api-gateway


【解决方案1】:

result 显然不在response 范围内,因此这将导致典型的undefined 错误。

解决方案是处理axios.get回调中的逻辑,试试这个:

const axios = require('axios');
const cheerio = require('cheerio');

exports.lambdaHandler = (event, context) => {
  axios.get('https://www.kitco.com')
    .then((response) => {
      const html = response.data;
      const $ = cheerio.load(html);
      const ask = $('#AU-ask').text();
      const bid = $('#AU-bid').text();
      const resbid = bid.slice(0, 7);
      const resask = ask.slice(0, 7);

      const result = {
        statusCode: 200,
        body: {
          ask: resask,
          bid: resbid
        }
      };

      console.log(result);
    })
    .catch(err => {
      console.log(err);
    });
};

【讨论】:

    【解决方案2】:

    您可以在 Lambda 控制台 Web 的监控选项卡中获取错误详细信息。我请您在return response 行中返回类似response is undefined 的错误。

    使用您的代码,return response 行将在您调用函数时立即执行,但 response 未在 lambdaHandler 范围内定义。

    我建议,不要将async/await 语法与 Promise 语法(.then .catch)混用,只使用其中一种,我建议使用async/await 语法。

    函数会喜欢:

    exports.lambdaHandler = async (event, context) => {
      try {
        const axios = require('axios');
        const cheerio = require('cheerio');
        const response = await axios.get('https://www.kitco.com'); // wait until we get the response
    
        const html = response.data;
        const $ = cheerio.load(html);
        const ask = $('#AU-ask').text();
        const bid = $('#AU-bid').text();
        const resbid = bid.slice(0, 7);
        const resask = ask.slice(0, 7);
    
        const result = {
          "ask": resask,
          "bid": resbid
        }
    
        return {
          statusCode: 200,
          body: JSON.stringify(result), // If you working with lambda-proxy-integrations, the `body` must be a string
        }; // return to response the request
      } catch (err) {
        console.log(err);
        return {
          statusCode: 500, // Example, http status will be 500 when you got an exception
          body: JSON.stringify({error: err}),
        }
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2023-02-16
      • 2020-05-20
      • 1970-01-01
      • 2022-09-28
      相关资源
      最近更新 更多