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.