0

I want to add row to Amazon DDB table from node app deployed on Zeit-now every time I get a post reqest but after sending response to post request. My ddb.putItem stops as pending promise and no errors are logged. I don't understand why.

The app is a slack bot. I get a message from slack api and it fires response by my bot. I want to quickly send 200 to slack to avoid getting message sent again. I tried different approaches, using EventEmitter or res.on('finish'... I did test that the module sending update to table in ddb works as when I fire it from command line node it works. But neither from now dev or deployed now app it does not.

I made a simplified test case in this repository: https://github.com/halas/now-test-case

Basically entrypoint for node app looks like this:

const send  = require('./send.js');

module.exports = (req, res) => {
  res.on('finish', send);
  res.end(`Hello from Node.js on Now 2.0!`);
  console.log('still here'); // this gets logged
};

And the send module:

[ require aws-sdk and set credentials ]

const ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
module.exports = () => {
  let params = {
    TableName: 'now-test-case',
    Item: {
      'id': { N: String(Date.now()) },
      'message': { S: 'hello World' },
    }
  };
  console.log('hello'); //we get here

  try {
  console.log('try'); //we get here
    const data = ddb.putItem(params).promise();
    console.log(data); //this is pending promise now
    data
      .then((data) => {console.log(data)})
      .catch((error) => {console.log(error)});
    // and it never gets resolved or rejected
  } catch(e) {
    console.log(e); //and no error catched here either
  }
};

As suggested by Rob Evans on Zeit Spectrum chat, I prepared the version with async-await (on branch in test repo), but results are the same.

I would expect to get the update on DynamoDB (resolved promise). While I get only pending promise and now resolve or reject.

halas
  • 1
  • 1
  • Ok, with help Paulo De Mitri [on Spectrum Chat](https://spectrum.chat/zeit/now/having-trouble-executing-amazon-ddb-call-from-node-deployed-on-node~6bee4adc-e30c-47cd-b3c2-17ab847b12dd) I think I understand the problem. Lambda function execution halts after response is sent, so I would need to invoke another lambda function, but this is not implemented directly in now, so another http endpoint probably. More here: https://spectrum.chat/zeit/now/invoke-lambda-from-another-lambda~daadc04d-e63f-49ee-8276-8d2ef906ec01 – halas May 22 '19 at 15:54
  • And here: https://stackoverflow.com/questions/44668605/aws-lambda-to-run-in-background-even-after-sending-response-to-api-gateway https://stackoverflow.com/questions/41102337/invoke-aws-lambda-and-return-response-to-api-gateway-asyncronously – halas May 22 '19 at 15:55

0 Answers0