简体   繁体   中英

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}

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.

The solution would be to handle the logic inside axios.get callback, try this:

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. I guest you get back an error like response is undefined in return response line.

With your code, return response line will be execute immediately when you call the function, but response did not defined in the lambdaHandler scope.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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