简体   繁体   English

内部服务器错误 aws lambda 函数 nodejs

[英]Internal server error aws lambda function nodejs

I am trying out a demo with AWS lambda function using Axios and Cheerio, I am getting back response after calling the endpoint as {message: Internal Server Error}我正在使用 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 

};

result is clearly not in response scope, therefore this will result in a typical undefined error. result显然不在response范围内,因此这将导致典型的undefined错误。

The solution would be to handle the logic inside axios.get callback, try this:解决方案是处理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);
    });
};

You can get error detail in monitor tab of Lambda console web.您可以在 Lambda 控制台 Web 的监视器选项卡中获取错误详细信息。 I guest you get back an error like response is undefined in return response line.我来宾你会得到一个错误,比如在return response行中response is undefined return response

With your code, return response line will be execute immediately when you call the function, but response did not defined in the lambdaHandler scope.使用您的代码,当您调用该函数时,将立即执行return response行,但response并未在lambdaHandler范围内定义。

I recommended that, don't mix async/await syntax with Promise syntax (.then .catch), just use one of them, I suggest use async/await syntax.我建议不要将async/await语法与 Promise 语法(.then .catch)混合使用,只使用其中之一,我建议使用async/await语法。

The function will like:该函数将喜欢:

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}),
    }
  }
};

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

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