I've got Sequelize-based code that runs fine in a Node.js project. I moved that code into an AWS Lambda handler and am testing it with the node-lambda module. Now the Sequelize code seems to be skipped. I'm not sure if the promise isn't being handled before the Lambda finishes, or if I'm missing something else. The following code skips the "during" console.log as shown in the output below.
var models = require('./models');
exports.handler = function( event, context, callback ) {
console.log("Before");
var body = JSON.parse(event.body);
// Find the device ID from the devices table
models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) {
console.log("During");
// Make sure that we got a device back
if (device === null) {
console.log("Device not found - could not insert data");
return;
}
else {
console.log("Device found, inserting data - " + body.device_uuid);
//Insert the data for this device
models.Data.create({
device_id: device.id,
data: body.data
});
}
});
console.log("After");
callback(null, "{\"status\": \"success\"}");
}
Yields...
Before
After
Success:
"{\"status\": \"success\"}"
Any ideas on where I'm going wrong? I'm using Node v5.9.0.