2
votes

I am having trouble with my lambda function. This is the current setup:

Lambda makes post request to API. API fetches data from postgres database and returns this data. When I use Postman or a local version of my lambda function, this works. When I use the actual lambda function, the API returns data with null.

Below are some code snippets:

lambda:

const axios = require('axios')

exports.handler = async (event) => {
  let rows = await axios.post('http:/server/getData', {
                params: {
                  var: event.var
                }
              })

  if(rows.data.statusCode == 204){
    //no data available
  }
  else{
    data = rows.data.info
  }
};

API Router Component

var router = express.Router()
const Pool = require('pg').Pool

const mgmtdb = new Pool({ ... })

router.post('/getData', function(req, res){
  database.query("SELECT info FROM table WHERE var=$1", [req.body.var], (error, results) => {
    const rows = results.rows

    if (error) {
      throw error
    }

    let status = rows.length == 0 ? 204 : 200
    var responseJSON ={};
    responseJSON.statusCode = status;
    responseJSON.info= rows[0] ? rows[0].info : null;
    res.json(responseJSON);
  })
})

module.exports = router

When I call the API from Postman I get statusCode: 200 (data available).

If I call the API with the exact same data from lambda I get statusCode: 204 (no data available).

I believe that this is some async timing problem. I don't know how the responses from the API can differ ..

Is it possible that the API streams the response back to the originator for some time not just an impulse? And starts by streaming "no data available" and then, after a few milliseconds "oh, i found some data, here it is"? And that Postman waits for the stream to finish and the lambda function doesn't?

Thanks in advance!

2
Could you please share the full functions of both lambda and API?idoshamun
@idoshamun Yes, I added more code to bothmatsbauer
try to add some logging in your API to check the variables you get from the Lambda function. Debug the process because on the surface it looks goodidoshamun

2 Answers

1
votes

Looks like your query is returning null because the req.body.var doesn't exist. Can you try changing your lambda function to this:

const axios = require('axios')

exports.handler = async (event) => {
  let rows = await axios.post('http:/server/getData', {
                  var: event.var
              })

  if(rows.data.statusCode == 204){
    //no data available
  }
  else{
    data = rows.data.info
  }
};

This basically removes the extra level params and makes the req.body.var work.

1
votes

Sometimes lambda have issue with async await so i suggest you to write code like this

  exports.handler = async (event, callback) => {
    axios.post('http:/server/getData', {
        var: event.var
    })
    .then((response) => {
        callback(null, {
            statusCode: 200,
            body: JSON.stringify(response)
        })
    })
};