2016-08-24 29 views
0

我有基於Sequelize的代碼在Node.js項目中運行良好。我將該代碼移入AWS Lambda處理程序,並使用節點lambda模塊對其進行測試。現在Sequelize代碼似乎被跳過了。我不確定是否在Lambda完成之前沒有處理這個承諾,或者如果我錯過了其他的東西。以下代碼跳過了「during」console.log,如下面的輸出所示。續集代碼不在AWS內部執行Lambda

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\"}"); 
} 

產量...

Before 
After 
Success: 
"{\"status\": \"success\"}" 

上我要去哪裏不對任何想法?我使用Node v5.9.0。

+0

我發現如果我刪除了Sequelize代碼運行良好的回調引用。 – user2174937

回答

3

我剛開始玩apigateway/lambda和sequelize,但據我所知node和sequelize,回調應該在「then」塊內。

昨天發現如果你使用回調函數(null,successData),性能非常差(在Select top 1上> 11秒)。必須更改標誌context.callbackWaitsForEmptyEventLoop = false,現在api調用需要24ms。

//Important to improve performance! 
    context.callbackWaitsForEmptyEventLoop = false 

    // 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) { 
      callback(new Error("Device not found - could not insert data")) 
     } 
     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 
      }) 
      .then(function(insertedData){ 
       callback(null, insertedData.toJSON())  
      }) 
      .catch(function(error){ 
       callback(new Error("Error creating") 
      }) 
     } 
    })  
    console.log("After") 
}